如何不返回UITableviewCell

问题描述 投票:0回答:2

我有一个 NSDictionaries 的 NSArray,在这个数组中有几个我不想在 UITableView 中显示的值,我想知道如何避免在

tableView:cellForRowAtIndexPath:
中返回这些单元格。

我尝试过

return nil;
但这导致了我的错误。

这就是我的代码的样子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    currentInstallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];

        NSNumber *tempDP = [currentInstallDictionary objectForKey:@"dp"];
        NSInteger myInteger = [tempDP integerValue];
        
        if (myInteger == 0) {

return cell;
}
return nil; // gives error
}
ios objective-c uitableview
2个回答
2
投票

此方法必须返回一个单元格。它不能返回零。最好的办法是在加载表之前过滤列表,并在使单元格出队时使用过滤后的数组。


0
投票

UITableView 仅要求一个单元格,因为您告诉它要询问。您对 UITableViewDataSource 协议的实现实现了两种方法:

在这些方法中,您可以确定屏幕上应显示多少个单元格。正如 Brian Shamblen 回答的那样,如果您不希望该数据出现在表视图中,则在计算部分和行数时如何忽略(过滤、删除等)该数据。如果您这样做,则不会要求“额外”单元格。

© www.soinside.com 2019 - 2024. All rights reserved.