我有两个不同的自定义 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/