【发布时间】:2012-07-13 10:46:16
【问题描述】:
我有一个UITableView,它使用数组来列出数据。这工作正常。
我还有一个UISearchBar 用于在那个tableview 中搜索。当 tableviews 数组中的数据匹配时,这些行被添加到另一个可变数组中,cellForRowAtIndexPath: 显示来自该可变数组的数据。
但是当我调用 reloadData 时,永远不会调用 numberOfRowsInSection:。因此,tableview 在滚动时崩溃,因为它试图从可变数组中获取数据,但是对于原始数组中的行(其中包含更多项)
我已经调试了很长时间,但看在上帝的份上,找不到原因。 Datasource 和 Delegate 在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;
}
【问题讨论】:
-
重新加载时检查表是否有内存..
标签: iphone objective-c uitableview