numberOfRowsInSection:未在reloadData上调用

Joh*_*ony 17 iphone objective-c uitableview

我有一个UITableView使用数组列出数据.这很好用.我还有一个用于在该tableview中搜索的UISearchBar.当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;
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*ess 11

如果numberOfSections返回0,numberOfRowsInSection将不会被调用.


Kru*_*lur 10

请确保不要reloadData从任何表视图的委托方法中调用.这将导致不可预测的行为.此外,请确保您之前没有调用beginUpdates过,并且只reloadData从主线程调用您的方法.


Muj*_*key 5

你怎么重装你的桌子?干得[table relaodData];好吗?您的桌子是否已连接到笔尖上的IBOutlet?在重新加载时,是否已table初始化?

你也可以发布崩溃信息.

  • 谢谢你的提示.不知何故,IB已将IBOutlet断开连接到xib中的UITableView.我没有注意到它没有连接,因为原始数据显示成功.但是,事后我意识到这不是因为引用插座,而是因为委托和数据源连接 (2认同)