我有一个使用数组列出数据的UITableView
。这可以很好地工作。我还有一个用于在该tableview
中进行搜索的UISearchBar
。当数据与tableviews数组中的数据匹配时,这些行将被添加到另一个可变数组中,cellForRowAtIndexPath:
将显示该可变数组中的数据。
但是当我调用reloadData时,numberOfRowsInSection:
永远不会被调用。因此,tableview
在滚动时崩溃,因为它试图从可变数组中获取数据,但获取的是原始数组中的行(包含更多项)
我已经调试了很长一段时间,但看在上帝的份上找不到原因。数据源和委派被挂接到tableview
中,因为它可以显示来自原始数组的数据。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"isSearching: %d", isSearching);
// Return the number of rows in the section.
if(!isSearching)
return [originalArray count];
NSLog(@"isSearching - Return searchCopyListOfItems");
return [searchCopyListOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if(!searching)
cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
else
cell.textLabel.text = [searchCopyListOfItems objectAtIndex:indexPath.row];
return cell;
}
发布于 2012-07-13 19:03:02
如何重新装入您的表?正确地做[table relaodData];
吗?您的桌面是否已连接到nib上的IBOutlet?在重新加载时,table
是否已初始化?
你也可以发布崩溃信息。
发布于 2017-04-15 21:13:57
如果numberOfSections
返回0
,则不会调用numberOfRowsInSection
。
发布于 2012-07-13 18:53:42
请确保不要从表视图的任何委托方法中调用reloadData
。这将导致不可预知的行为。此外,请确保您以前没有调用过beginUpdates
,并且只从主线程调用了reloadData
方法。
https://stackoverflow.com/questions/11468970
复制相似问题