UITableView cellforrowatindexpath not called
I am pretty new to Iphone development . so please bear me if I ask some very simple questions.
In my application I have multiple views(i.e. .xib files). On clicking the button on the main view(CouponWebsiteViewController.Xib) the application should load the second view(BrowseList.Xib) which contains a UITable and I have to populate that UITable with some data. I have written following code to populate the data on BrowseList.m file:
- (void) viewDidLoad{
arrayData = [[NSArray alloc] init];
arrayData = [arrayData arrayByAddingObject:@"dfsgdf"];
arrayData = [arrayData arrayByAddingObject:@"aaaaaa"];
self.lblNewScreen.text = [arrayData objectAtIndex:0];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayData 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]
autorelease];
}
NSString *cellValue = [arrayData objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
[tableView setEditing:YES animated:YES];
return cell;
}
But It is not populating the data in table , when I debug this code I found that it is not executing cellForRowAtIndexPath met开发者_运维问答hod but it is debugging numberOfRowsInSection and numberOfSectionsInTableView methods.
But the interesting thing is when I write the same code on the CouponWebsiteViewController.m (i.e. on main view) then it is populating the data in table.
The point is this code is working fine on the main view but it does not work for the other views.
Can any one tell me what am I missing or any other way to populate the UITable on views other than the main view.
Thanks in Advance. Gaurav
I'm not positive but here are some ideas.
- I think when you put that array code in
viewDidLoad
it's too late.ViewDidLoad
is executed after the data has already been placed in the table. Try putting this code in the initWithStyle method or whateverinit
method you have. Or you could even put it int theViewWillAppear
method, but then make sure to follow this next suggestion. - The other thing you could try is, just call
[self.tableView reloadData]
at the end of theviewDidLoad
method. Although that's not as ideal as the first suggestion.
Are you sure you set your class as the dataSource on the table? To do this, control+click on the table view in Interface Builder and drag it onto the File's Owner under Placeholders. Then select dataSource (and probably delegate too, depending on your controller-view setup).
I suggest you make sure that the size of the table requires rows to be displayed.
Indeed, it could be a case for why the method cellForRowAtIndexPath
is not called, however the numberOfRowsInSection
should be called.
In this case you'll not see anything in your table.
For anyone that comes across this problem, I solved this by adding my TableView's controller as a child to the parent controller using:
UITableViewController * table = [[UITableViewController alloc] init];
...
[self.view addSubview:table.tableView];
[self addChildViewController:table];
Old question, but I'll chime in. For others.
There are a number of reasons why a tableview doesn't update and that cellForRowAtIndexPath: doesn't get called.
If you're using a UITableViewController directly then it amy be that the "numberOfRowsInSection:" method is returning 0
If you're using a UItableView in your view controller. chances are you haven't adopted the correct protocols and implemented the right methods - plus if you're using a xib or storyboard that you haven't assigned the right delegate and datasource is the other potential problem.
Finally if you're using a subclass of UITableViewController then it's possible that you haven't overwritten one of the default methods and again numberOfRowsInSection is returning 0.
Initialize UITableView in viewWillAppear...
I've encounter some similar problem there. The table view delegate and data source is set properly, and (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
also returns a positive count. But - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is never called.
I've tried the methods before, but It doesn't work neither. Finally, I've noticed that I've forgotten to add the table view as a subview of my view controller. After doing that, it works like a charm.
Did you make sure to set the table view as the delegate?
If you have a UITableViewController, you this need in viewDidLoad:
[self.tableView setDelegate:self];
If you have another type of view controller, just make the UITableView an IBOutlet, make the connections to the file owner for tableViewDelegate and tableViewDataSource then do this in viewDidLoad:
[self.myTableProperty setDelegate:self];
Also make sure to conform to the UITableViewDelegate and UITableViewDataSource protocols in your .h file if the View Controller is not a UITableViewController
Cheers
If the mutable array or the array you are using to populate the contents of the cell is set as a property in your header file. then make sure you always use
self.yourArray = [[NSMutableArray alloc] init];
[self.yourArray addObject:@"Object1"];
and then to get the count in numberOfRowsInSection as well you need to access it as:
return [self.yourArray count];
This is pretty much like the this pointer you use to refer to your instance variables. And yes of course you also need to keep in mind if you have set the dataSource and delegate of the UITableView to the file's owner. Also that you have included the UITableViewDelegate and UITableViewDataSource in your header file of the viewController.
I hope it helps you. Best Of Luck. Happy Coding! Cheers!!
Well, I can add one simple point for everyone with this problem. I would recommend using debug and making sure that:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
does not return 0. If this function is not implemented or if it returns 0, the method:
cellForRowAtIndexPath:(NSIndexPath *)indexPath
is not called at all since there are no cells to be displayed.
Check that the tableview has been added to the view. If not then other table view delegate and datasource methods will be fired but not cellforrow :)
I was also having that problem on UITableView, I also set the data source and delegate to the table view but my cellForRowAtIndexPath
method was not called.
Solution:
my network blocked my method (by using firewall). I asked my network admin, and I was given full access to the network, and then my
cellForRowIndexPath
method was called.when my response comes from a web service, due to network block response was black so,
cellForRowIndexPath
method was not called.
精彩评论