IOS - cellForRowAtIndexPath跳过行

Nug*_*get 7 iphone objective-c uitableview ios

基本上,该cellForRowAtIndexPath函数需要返回一个UITableViewCell.在我的代码中,我想检查一个将检查某些内容的行为,并在找到特定值时跳过单元格.

这就是我现在所拥有的:

static NSString *FirstCellIdentifier = @"First Custom Cell";
static NSString *SecondCellIdentifier = @"Second Custom Cell";

CustomObject *o = [_customObjects objectAtIndex:indexPath.row];

if ([s.name isEqualToString:@""])
{
    FirstCellController *cell = [customList dequeueReusableCellWithIdentifier:FirstCellIdentifier];
    if (!cell) { 
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (FirstCellController *) currentObject;
                break;
            }
        }
    }
    // Here I do something with the cell's content
    return cell;
}
else {
    SecondCellController *cell = [customList dequeueReusableCellWithIdentifier:SecondCellIdentifier];
    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SecondCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (SecondCellController *) currentObject;
                break;
            }
        }
    }
    // Here i do something with the cell's content
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我想做的是,如果s.name它不是空的,我想"跳过"单元格,而不是显示它并转到下一个单元格.

有人对此有一些建议吗?

谢谢.

Tom*_*ift 19

你不能以这种方式"跳过"一个单元格.如果您的数据源声称有n行,那么您必须为每个行提供一个单元格.正确的方法是修改数据源以(n-1)在要删除时声明行,然后调用UITableView reloadData让它重新生成表(并要求为每个可见行提供新单元格).

另一种选择是"隐藏"行/单元格.我用于此的技术是通过heightForRowAtIndexPath以下方式为给定单元格提供0的高度:

  • 如果内容仍然显示,则单元格不会剪切到它的帧.在cellForRowAtIndexPath中:创建单元格时,设置cell.clipsToBounds = YES; (5认同)