简体   繁体   中英

cellForRowAtIndexPath not being called on tableView reloadData

I have a UITableView on one view that loads in data at the start of the application, and then later when a user enters text into a box and hits a button, I re-query the database, re-populate the original NSMutableArray that stores the data for the table.

All of that is working perfectly. In some logging statements I can tell that the array has the correct information, the numberOfRowsInSection method is returning the proper count, and is being called after the reload is called.

However, the cellForRowAtIndexPath is never called the second time (after the reload) and the table data is never updated.

I've spent hours searching the net and I've found nothing that helps. Can anyone help?

All code is at: http://github.com/botskonet/inmyspot

The specific reload is being called at: http://github.com/botskonet/inmyspot/blob/master/Classes/InMySpotViewController.m

Roughly Line 94

From: http://github.com/botskonet/inmyspot/blob/master/Classes/PlateFormViewController.m

Roughly line 101

A bit more info: once the new data has been added to the mutablearray, if I try to start scrolling the table, it eventually dies with:

"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (29) beyond bounds (29)'"

Which I assume means the table cells can't find any data in the array to match the scroll position, which seems to be because the array has the new data, but the table doesn't.

I was seeing the same issue. My problem was similar: I have a UIViewController subclass (MyViewController) that contains a few things, including a reference to my subclass of UITableViewController (MyTableViewController) . In MyViewController.xib , in order to make sure that the tableView that is loaded by the MyViewController is the same one as the one loaded in MyTableViewController , in Interface Builder I hooked up the reference from the tableView in MyViewController and made it point to MyTableViewController's view property.

Here's a picture, since actions in Interface Builder are so hard to describe: 在此处输入图片说明

Your reload is not being called because you haven't setup your delegates for UITableView properly.

@interface InMySpotViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
}

Also you overwrite mainDelegate.plates here:

NSMutableArray *platesArray = [[NSMutableArray alloc] init];
                mainDelegate.plates = platesArray;
                [platesArray release];

Why not combine your two controllers. The way your doing it is leading to most of your problems. You create a new InMySpotViewController unnecessarily in PlateFormViewController.

In plateLookup you clobber the existing array:

    NSMutableArray *platesArray = [[NSMutableArray alloc] init];
    mainDelegate.plates = platesArray;
    [platesArray release];

After this you reload the table. The array has zero elements so cellForRowAtIndexPath: is never called.

Edit: You also create a new view controller

        InMySpotViewController *viewController = [[InMySpotViewController alloc] init]; 
        [viewController refreshTable];

So you're not even reloading the table that's visible on the screen.

Thanks to some debugging by a family member I've determined the following issues were the cause:

The inMySpotViewController was setup as a UITableViewController not a UIViewController which is why the table was covering up everything else. I changed it to a UIViewController and then added a new UITableView :

IBOutlet UITableView *myTblView;
@property (nonatomic, retain) UITableView *myTblView;

And then referenced that table view throughout my code. This code was done on the single-view branch so it's now working properly.

The platesArray kept being mentioned as a problem, but it's not because the out of bounds was happening because the array was successfully being updated, whereas the table view was not (so I was trying to scroll to an index in the array that didn't exist, but I knew that was the problem since they table didn't visually update).

I had successfully logged out the contents of the revised plates array after re-writing it's values and they were all correct, and plates.count was returning the proper values, so it was not getting "clobbered" in anyway.

Thanks everyone for the help!

I had the same problem. Most of the answers from google talked about delegate . But I was sure that my delegate and data source were set properly.

Latter, I found that the problem is the return value of this method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

In my code, I rewrote the method like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _dataArray.count;
}

But when checking this break point, I found _dataArray is nil somehow. This method should return a positive integer, otherwise

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

might not be called.

I hope it can be helpful.

I had the same problem, and I traced it to having a tableView property in the interface. I don't think I added this myself but who cares, it was masking the UITableViewController's own tableView property. I removed it, the project continued to build, but now when I return from the pushed viewcontroller back to the first UITableViewController, the proper sequence happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM