在TableView中有选择地加载单元 [英] Load cell selectively in TableView

查看:60
本文介绍了在TableView中有选择地加载单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在某些条件为真时,我才需要在tableView:cellForRowAtIndexPath:中返回单元格,例如:

I need to return cell in tableView:cellForRowAtIndexPath: only when some condition is true,for example:

if (condition == true)
return nil;
else
return cell;

返回nil会给我一个错误。

Returning nil gives me an error.

推荐答案

您需要做更多的工作才能有条件地在UITableView中有一个单元格。

You'll need to do a little more to conditionally have a cell in a UITableView.

假设您有3个单元格,第一个单元格是有条件的。首先需要根据以下条件使表具有2个或3个单元格:

Let's assume you have 3 cells, and the first one is conditional. The first thing you need to do make your table have either 2 or 3 cells based on this condition:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(condition) {
        return 3;
    } else {
        return 2;
    }
}

接下来,您可以返回实际的单元格。

Next you can return the actual cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(condition) {
        if(indexPath.row == 0) {
            //create and return conditional cell
        } else {
            //create and return normal cell
        }
    } else {
        //create and return normal cell
    }
}

然后瞧!

PS :(这是假设所有操作都在一个部分中完成。如果您需要将其分解为多个部分,我们可以得到也是如此)。

PS: (This is assuming everything is done in a single section. If you need to break that out into multiple sections we can get into that as well).

这篇关于在TableView中有选择地加载单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆