iphone - IOS - cellForRowAtIndexPath 跳过行

标签 iphone objective-c ios uitableview

基本上,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;
}

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

有人对此有什么建议吗?

谢谢。

最佳答案

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

另一种选择是“隐藏”行/单元格。我为此使用的技术是通过 heightForRowAtIndexPath 为给定的单元格提供 0 的高度:

关于iphone - IOS - cellForRowAtIndexPath 跳过行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843681/

相关文章:

iphone - for循环递增

ios - 在 UINavigationController 中设置框架 View Controller View

ios - WKInterfaceGroup之间的垂直间距

iOS - 核心动画 - 如何在变换属性更改时设置原点

iphone - 相对于发件人选择的附件,如何在推送(带有 segue) View 上更改 UILabel 的文本?

ios - 在不使用自动布局或制作新的 xib 的情况下从 iPhone 5 调整 iPhone 4 的大小

objective-c - 如何在iOS中从字符串中切出字符串?

ios - 在哪里可以找到 iOS 用户体验导航最佳实践?

iphone - 使用performselector方法将多个 View Controller 作为 View 依次调用

iphone - 清理 Objective-C 代码