简体   繁体   English

在tableView reloadData上未调用cellForRowAtIndexPath

[英]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. 我在一个视图上有一个UITableView,该视图在应用程序启动时加载数据,然后稍后当用户在框中输入文本并单击按钮时,我重新查询数据库,重新填充存储NFSutableArray的原始NSMutableArray。表的数据。

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. 在某些日志记录语句中,我可以说出数组具有正确的信息,numberOfRowsInSection方法返回正确的计数,并在调用重载后被调用。

However, the cellForRowAtIndexPath is never called the second time (after the reload) and the table data is never updated. 但是,永远不会第二次调用cellForRowAtIndexPath(在重新加载之后),并且永远不会更新表数据。

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 所有代码位于: http : //github.com/botskonet/inmyspot

The specific reload is being called at: http://github.com/botskonet/inmyspot/blob/master/Classes/InMySpotViewController.m 具体的重载在以下位置调用: http : //github.com/botskonet/inmyspot/blob/master/Classes/InMySpotViewController.m

Roughly Line 94 粗线94

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

Roughly line 101 粗线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: 更多信息:将新数据添加到mutablearray中后,如果我尝试开始滚动表,它将最终死于:

"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (29) beyond bounds (29)'" “由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***-[NSCFArray objectAtIndex:]:索引(29)超出范围(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) . 我的问题很相似:我有一个UIViewController子类(MyViewController) ,其中包含一些内容,包括对我的UITableViewController (MyTableViewController)子类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. MyViewController.xib ,为了确保由MyViewController加载的tableView与在MyTableViewController加载的tableView相同,在接口生成器中,我从MyViewController的tableView中引用了该引用,并使其指向MyTableViewController's视图。属性。

Here's a picture, since actions in Interface Builder are so hard to describe: 这是一张图片,因为Interface Builder中的动作很难描述: 在此处输入图片说明

Your reload is not being called because you haven't setup your delegates for UITableView properly. 由于尚未正确设置UITableView的委托,因此未调用您的重新加载。

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

Also you overwrite mainDelegate.plates here: 您还可以在此处覆盖mainDelegate.plates:

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. 您不必要在PlateFormViewController中创建一个新的InMySpotViewController。

In plateLookup you clobber the existing array: plateLookup ,破坏现有数组:

    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. 该数组有零个元素,因此从不调用cellForRowAtIndexPath:

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. inMySpotViewController设置为UITableViewController而不是UIViewController ,这就是表格掩盖其他所有内容的原因。 I changed it to a UIViewController and then added a new UITableView : 我将其更改为UIViewController ,然后添加了新的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). PlatesArray一直被认为是一个问题,但这不是因为越界越好是因为数组已成功更新,而表视图却没有(所以我试图滚动到数组中没有索引的索引存在,但我知道这是问题所在,因为它们的表没有进行视觉更新)。

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. 重写值后,我已经成功注销了修改后的Plates数组的内容,并且它们都是正确的,而Plates.count返回的是正确的值,因此无论如何它都不会变得“混乱”。

Thanks everyone for the help! 谢谢大家的帮助!

I had the same problem. 我有同样的问题。 Most of the answers from google talked about delegate . 谷歌的大多数答案都谈到了delegate But I was sure that my delegate and data source were set properly. 但是我确定我的delegatedata source设置正确。

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. 但是当检查此断点时,我发现_dataArray存在。 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. 我遇到了同样的问题,并且我追踪到它在接口中具有tableView属性。 I don't think I added this myself but who cares, it was masking the UITableViewController's own tableView property. 我不认为我自己添加了这个,但谁在乎,它掩盖了UITableViewController自己的tableView属性。 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. 我将其删除,该项目继续构建,但是现在当我从推入的viewcontroller返回第一个UITableViewController时,会发生正确的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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