I am creating an application in which I am having a UITableviewController
class and in that I am having a IB file with a UITableView
in it. In my UIViewController
class I am creating a UIPopOverController
and assigning a navigation controller to the contentview of that popover.In the navigation controller the rootView is the UITableView
Controller Object. The problem which I am working is the display of the PopOver on the click of a Bar Button which is showing a UITableView
in it. The Problem is that it is showing only the Blank TableView as the delegate and Datasource methods for the TableView are never getting Called. This I have checked while Debugging. I am Pasting my code here. Please do reply that why my delegate and datasource methods are not getting called.
This is 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;
}
I have also declared the IBOutlet of the TableView in the TableViewController Class. The Datasource and delegate protocols have been mentioned in the ViewController.h class. Please Help me and forgive me if the way of asking the question is wrong.
Issue is due to setting the delegate before presenting the table view. Now just replace this like
settingstable.tbl_settings.delegate = self;
settingstable.tbl_settings.dataSource = self;
[settingstable.tbl_settings reloadData];
after presenting the pickerview like this.
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];
This working fine for me..