![](/img/trans.png)
[英]tableView reloadData not working: cellForRowAtIndexPath not called
[英]cellForRowAtIndexPath not being called on tableView reloadData
我在一个视图上有一个UITableView,该视图在应用程序启动时加载数据,然后稍后当用户在框中输入文本并单击按钮时,我重新查询数据库,重新填充存储NFSutableArray的原始NSMutableArray。表的数据。
所有这些都运行良好。 在某些日志记录语句中,我可以说出数组具有正确的信息,numberOfRowsInSection方法返回正确的计数,并在调用重载后被调用。
但是,永远不会第二次调用cellForRowAtIndexPath(在重新加载之后),并且永远不会更新表数据。
我已经花了几个小时在网上搜索,但没有发现任何有用的信息。 有人可以帮忙吗?
所有代码位于: http : //github.com/botskonet/inmyspot
具体的重载在以下位置调用: http : //github.com/botskonet/inmyspot/blob/master/Classes/InMySpotViewController.m
粗线94
来自: http : //github.com/botskonet/inmyspot/blob/master/Classes/PlateFormViewController.m
粗线101
更多信息:将新数据添加到mutablearray中后,如果我尝试开始滚动表,它将最终死于:
“由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***-[NSCFArray objectAtIndex:]:索引(29)超出范围(29)”'”
我认为这意味着表单元格无法在数组中找到任何数据来匹配滚动位置,这似乎是因为数组具有新数据,而表却没有。
我看到了同样的问题。 我的问题很相似:我有一个UIViewController子类(MyViewController)
,其中包含一些内容,包括对我的UITableViewController (MyTableViewController)
子类UITableViewController (MyTableViewController)
的引用。 在MyViewController.xib
,为了确保由MyViewController
加载的tableView与在MyTableViewController
加载的tableView相同,在接口生成器中,我从MyViewController
的tableView中引用了该引用,并使其指向MyTableViewController's
视图。属性。
由于尚未正确设置UITableView的委托,因此未调用您的重新加载。
@interface InMySpotViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
}
您还可以在此处覆盖mainDelegate.plates:
NSMutableArray *platesArray = [[NSMutableArray alloc] init];
mainDelegate.plates = platesArray;
[platesArray release];
为什么不合并两个控制器。 您的操作方式会导致大多数问题。 您不必要在PlateFormViewController中创建一个新的InMySpotViewController。
在plateLookup
,破坏现有数组:
NSMutableArray *platesArray = [[NSMutableArray alloc] init];
mainDelegate.plates = platesArray;
[platesArray release];
此后,您重新加载表。 该数组有零个元素,因此从不调用cellForRowAtIndexPath:
编辑:您还创建一个新的视图控制器
InMySpotViewController *viewController = [[InMySpotViewController alloc] init];
[viewController refreshTable];
因此,您甚至不需要重新加载屏幕上可见的表。
由于家庭成员进行了调试,我确定了导致以下问题的原因:
inMySpotViewController
设置为UITableViewController
而不是UIViewController
,这就是表格掩盖其他所有内容的原因。 我将其更改为UIViewController
,然后添加了新的UITableView
:
IBOutlet UITableView *myTblView;
@property (nonatomic, retain) UITableView *myTblView;
然后在整个代码中引用该表视图。 该代码是在单视图分支上完成的,因此现在可以正常工作。
PlatesArray一直被认为是一个问题,但这不是因为越界越好是因为数组已成功更新,而表视图却没有(所以我试图滚动到数组中没有索引的索引存在,但我知道这是问题所在,因为它们的表没有进行视觉更新)。
重写值后,我已经成功注销了修改后的Plates数组的内容,并且它们都是正确的,而Plates.count返回的是正确的值,因此无论如何它都不会变得“混乱”。
谢谢大家的帮助!
我有同样的问题。 谷歌的大多数答案都谈到了delegate
。 但是我确定我的delegate
和data source
设置正确。
后来,我发现问题在于此方法的返回值:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
在我的代码中,我重写了如下方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _dataArray.count;
}
但是当检查此断点时,我发现_dataArray
存在。 此方法应返回一个正整数,否则
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
可能不会被调用。
希望对您有所帮助。
我遇到了同样的问题,并且我追踪到它在接口中具有tableView属性。 我不认为我自己添加了这个,但谁在乎,它掩盖了UITableViewController自己的tableView属性。 我将其删除,该项目继续构建,但是现在当我从推入的viewcontroller返回第一个UITableViewController时,会发生正确的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.