UITableView cellforrowatindexpath not called

I am very new to Iphone development. so please take me if I ask very simple questions.

In my application, I have several types (for example, .xib files). When I click a button on the main screen (CouponWebsiteViewController.Xib), the application should load the second view (BrowseList.Xib) that contains UITable, and I have to fill this UITable with some data. I wrote the following code to populate the data in the 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 does not fill in the data in the table, when I debug this code, I found that it does not execute the cellForRowAtIndexPath method, but it debugs the numberOfRowsInSection and numberOfSectionsInTableView methods.

But interestingly, when I write the same code on CouponWebsiteViewController.m (i.e. on the main view), it fills the data in the table.

The fact is that this code works fine on the main view, but it does not work for other views.

Can someone tell me what I am missing or some other way to fill in a UITable on views other than the main view.

Thanks at Advance. Gaurav

+6
uitableview
source share
12 answers

I'm not sure, but here are some ideas.

  • I think when you put this array code in viewDidLoad too late. viewDidLoad is executed after the data has already been placed in the table. Try putting this code in the initWithStyle method or any other init method you have. Or you can even put it in the ViewWillAppear method, but then be sure to follow this next sentence.
  • Another thing you can try is simply call [self.tableView reloadData] at the end of the viewDidLoad method. Although this is not as perfect as the first sentence.
+9
source share

Are you sure you set your class as the data source in the table? To do this, control + click on the table view in Interface Builder and drag it into the File Owner in the Placeholders section. Then select dataSource (and maybe a delegate too, depending on your controller settings).

+5
source share

I suggest you make sure that the size of the table requires the display of rows.

In fact, this may be the case why the cellForRowAtIndexPath method cellForRowAtIndexPath not called, however, call numberOfRowsInSection .

In this case, you will not see anything in your table.

+4
source share

Initialize a UITableView in viewWillAppear ...

+3
source share

For those facing this problem, I solved this by adding a TableView controller as a child to the parent controller using:

 UITableViewController * table = [[UITableViewController alloc] init]; ... [self.view addSubview:table.tableView]; [self addChildViewController:table]; 
+3
source share

Old question, but I will call. For others.

There are a number of reasons why the table view is not updated and that cellForRowAtIndexPath: does not receive the call.

If you use the UITableViewController directly, this means that the numberOfRowsInSection: method returns 0

If you use UItableView in your view controller. most likely you did not accept the correct protocols and implement the correct methods - plus if you use xib or a storyboard that you did not assign to the right delegate, and the data source is another potential problem.

Finally, if you use a subclass of UITableViewController, then it is possible that you did not overwrite one of the default methods, and again numberOfRowsInSection returns 0.

+2
source share

I have some kind of similar problem. The table view delegate and data source are set correctly, and (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section also returns a positive value. But - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath never called.

I already tried the methods before, but this does not work. Finally, I noticed that I forgot to add a table view as a sub element of my view controller. After that, it works like a charm.

+1
source share

Are you sure you are defining the table as a delegate?

If you have a UITableViewController, you need it in viewDidLoad:

 [self.tableView setDelegate:self]; 

If you have a different type of view controller, just make a UITableView IBOutlet, make connections to the file owner for tableViewDelegate and tableViewDataSource, then do it in viewDidLoad:

 [self.myTableProperty setDelegate:self]; 

Also make sure that you follow the UITableViewDelegate and UITableViewDataSource protocols in your .h file if the View Controller is not a UITableViewController

Greetings

0
source share

If the volatile array or the array that you use 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 counter in numberOfRowsInSection, you also need to access it:

 return [self.yourArray count]; 

This is very similar to this pointer that you use to reference instance variables. And yes, of course, you also need to keep in mind if you set the dataSource and the UITableView delegate to the file owner. In addition, you have included UITableViewDelegate and UITableViewDataSource in your viewController header file.

Hope this helps you. Good luck. Happy coding! Hooray!!

0
source share

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 

not called at all, as there are no cells to be displayed.

0
source share

Make sure the table view is added to the view. If not, then other methods for representing delegates and data source methods will be launched, but not cellular technology :)

0
source share

I also had a problem with UITableView, I also set the data source and delegated it to the table view, but my cellForRowAtIndexPath method cellForRowAtIndexPath not called.

Decision:

  • my network blocked my method (using a firewall). I asked the network administrator and I was given full access to the network, and then my cellForRowIndexPath method was called.

  • when my answer comes from a web service, because the network block response was black, so the cellForRowIndexPath method cellForRowIndexPath not called.

-one
source share

All Articles