1

我正在创建一个应用程序,其中我有一个UITableviewController类,并且我有一个 IB 文件,其中包含一个UITableView。在我的UIViewController课程中,我正在创建一个UIPopOverController导航控制器并将其分配给该弹出框的内容视图。在导航控制器中,rootView 是UITableView控制器对象。我正在处理的问题是在单击其中显示 a 的 Bar Button 时显示 PopOver UITableView。问题是它只显示空白表视图,因为表视图的委托和数据源方法永远不会被调用。这是我在调试时检查过的。我在这里粘贴我的代码。请回答为什么我的委托和数据源方法没有被调用。

这是 ViewController.m:-

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
      arr_settings = [[NSMutableArray alloc]           
      initWithObjects:@"shikhar",@"gulshan",@"bhargav", nil];

       //tbl_settings.delegate = self;
      // tbl_settings.dataSource = self;
      //self.title = @"Settings";
     //arr_settings = [[NSMutableArray alloc] 
     initWithObjects:@"shikhar",@"gulshan",@"bhargav", nil];

     // Custom initialization
   }
     return self;
 }

 -(IBAction)btn_settings:(id)sender{
  TableViewController * settingstable = [[TableViewController alloc] 
  initWithNibName:@"TableViewController" bundle:Nil];
  settingstable.tbl_settings.delegate = self;
  settingstable.tbl_settings.dataSource = self;
  //settingstable.tbl_settings.transform = CGAffineTransformMakeScale(1, 1);
  //settingstable.tbl_settings.tag = 1;
  //[self.view addSubview:settingstable.view];
  UINavigationController *nav = [[UINavigationController alloc]
  initWithRootViewController:settingstable];
  UIPopoverController * pickerPopover = [[UIPopoverController alloc] 
  initWithContentViewController:nav];

  pickerPopover.delegate = self;
  self.popovercontroller = pickerPopover;
  //CGRect popoverRect = [self.view convertRect:[_btn_settings frame] fromView: 
  [_btn_settings superview]];
  //popoverRect.size.width = MIN(popoverRect.size.width, 100);
  pickerPopover.popoverContentSize = settingstable.tbl_settings.frame.size;
  [settingstable.tbl_settings reloadData];
  [pickerPopover presentPopoverFromBarButtonItem:sender  
  permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
 }
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      return 1;
    }

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
    (NSInteger)section
    {
       NSLog(@"%u",[arr_settings count]);
       return [arr_settings count];

    }

   // Customize the appearance of table view cells.
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
     (NSIndexPath *)indexPath {
          static NSString *CellIdentifier = @"Cell";

          UITableViewCell *cell = [tableView  
          dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier];
          if ([[UIDevice currentDevice] userInterfaceIdiom] ==  
          UIUserInterfaceIdiomPhone) {
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         }
     }


          cell.textLabel.text = [arr_settings objectAtIndex:indexPath.row];
          NSLog(@"%@",cell.textLabel.text);
          return cell;

      }

我还在 TableViewController 类中声明了 TableView 的 IBOutlet。在 ViewController.h 类中已经提到了 Datasource 和 delegate 协议。如果提问的方式不对,请帮助我并原谅我。

4

1 回答 1

0

问题是由于在呈现表视图之前设置委托。现在只需像这样替换它

settingstable.tbl_settings.delegate = self;
settingstable.tbl_settings.dataSource = self;

[settingstable.tbl_settings reloadData];

像这样展示pickerview之后。

    TableViewController * settingstable = [[TableViewController alloc]
                                           initWithNibName:@"TableViewController" bundle:Nil];

    UINavigationController *nav = [[UINavigationController alloc]
                                   initWithRootViewController:settingstable];
    UIPopoverController * pickerPopover = [[UIPopoverController alloc]
                                           initWithContentViewController:nav];

    pickerPopover.delegate = self;
    self.popovercontroller = pickerPopover;

   [_btn_settings superview]];

    pickerPopover.popoverContentSize = settingstable.tbl_settings.frame.size;
    [pickerPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    settingstable.tbl_settings.delegate = self;
    settingstable.tbl_settings.dataSource = self;

    [settingstable.tbl_settings reloadData];

这对我来说很好..

于 2013-03-29T10:43:50.630 回答