PopOverController中的TableView

时间:2013-03-29 06:09:21

标签: cocoa-touch uitableview uipopovercontroller

我正在创建一个应用程序,其中我有一个UITableviewController类,并且我有一个带有UITableView的IB文件。在我的UIViewController类中,我创建了一个UIPopOverController并将导航控制器分配给该popover的contentview。在导航控制器中,rootView是UITableView控制器对象。我正在工作的问题是单击一个条形按钮显示PopOver,其中显示UITableView。问题是它只显示Blank TableView作为委托,而TableView的Datasource方法永远不会被调用。这是我在调试时检查的。我在这里粘贴我的代码。请回答为什么我的委托和数据源方法没有被调用。

这是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类中提到了数据源和委托协议。如果提出问题的方式是错误的,请帮助我并原谅我。

1 个答案:

答案 0 :(得分:0)

问题是由于在呈现表视图之前设置了委托。现在只需将其替换为

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

[settingstable.tbl_settings reloadData];

在呈现这样的选择视图之后。

    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];

这对我来说很好..