ios - Obj-c - 同时返回两个自定义单元格

标签 ios objective-c tableview

我有两个不同的自定义 TableView 单元格。也就是说,当返回第一个单元格类型时,我希望在之后立即返回另一个单元格类型。从技术上讲,下面的代码是我想要发生的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

        UniversalAlertTableViewCell *cellUni = (UniversalAlertTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifierUni forIndexPath:indexPath];

        return cell;
        return cellUni;
    }

但是,正如我们所知,代码在第一个返回单元格后停止执行。我怎样才能做到这一点?

最佳答案

您一次只能返回一种特定类型。使用 indexPath 参数来决定返回哪一个。

以下代码是您所需要的粗略概念。您可以根据自己的实际需要调整 if 语句。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

        // configure cell based on data from your data model for this indexPath

        return cell;
    } else {
        UniversalAlertTableViewCell *cell = (UniversalAlertTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifierUni forIndexPath:indexPath];

        // configure cell based on data from your data model for this indexPath

        return cell;
    }
}

关于ios - Obj-c - 同时返回两个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054080/

相关文章:

objective-c - 在 iOS7 中出现键盘时调整 UITextView 的大小

arrays - 自定义TableView错误

swift - 创建没有 tableView 部分的 indexList?

ios - FRC 内容更改时 UITableView 异常

python - 安装核心 ML 工具时出错

objective-c - 当委托(delegate)不再被引用时,在 `[NSApp run]` 处崩溃

pagination - 如何在JavaFX中实现TableView的分页?

ios - 使用 arm-apple-darwin10-llvm-gcc-4.2 在 MacOS 上交叉编译问题

iphone - 地址簿联系人的唯一记录标识符 + SQLite 数据库

ios - 如何让将以模态方式呈现的 UIViewController 的 UIView 始终出现在屏幕底部?