我有一个使用数组列出数据的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;
}
https://stackoverflow.com/questions/11468970
复制相似问题