简体   繁体   English

如何配置 cellForRowAtIndexPath 来处理两种不同的自定义 UITableViewCell 类型

[英]How to configure cellForRowAtIndexPath to handle two different custom UITableViewCell types

I am trying to use two different custom UITableViewCell's but am struggling with how to get this done.我正在尝试使用两种不同的自定义 UITableViewCell,但正在努力解决如何完成这项工作。 I have the following but think that everything needs to be called cell.我有以下内容,但认为一切都需要称为单元格。 Also, I'm not sure if my return type is correct.另外,我不确定我的返回类型是否正确。 What is the canonical way to set this up?设置它的规范方法是什么?

This is a follow-up to this question (although not fully relevant).这是这个问题的后续(尽管不完全相关)。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuHeaderCellIdentifier=@"HeaderCell";
  static NSString *MenuItemCellIdentifier=@"ItemCell";

  HeaderCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
  ItemCell *miCell = [self.menuTV dequeueReusableCellWithIdentifier:ItemCellIdentifier];
  
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[Header class]]){
    Header *menuHeader=(Header *)dic;
    cell.nameLabel.text=@"here";
    return cell;
  }else if([dic isKindOfClass:[Item class]]){
    Item *item=(Item *)dic;
    miCell.headerLabel.text=@"here";
    return miCell;
  }

Assuming that you are using prototype cells in your Storyboard, or that you have otherwise registered your cell identifiers, then your code is almost right.假设您在 Storyboard 中使用原型单元格,或者您以其他方式注册了单元格标识符,那么您的代码几乎是正确的。 You should only dequeue the type of cell you need -你应该只出列你需要的单元格类型 -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *menuHeaderCellIdentifier=@"HeaderCell";
  static NSString *menuItemCellIdentifier=@"ItemCell";

  id dic=self.tmpMenu.listItems[indexPath.row];

  if([dic isKindOfClass:[Header class]]) {
    HeaderCell *headerCell = [self.menuTV dequeueReusableCellWithIdentifier:menuHeaderCellIdentifier];
    Header *menuHeader=(Header *)dic;
    headerCell.nameLabel.text=@"here";
    return headerCell;
  } else if([dic isKindOfClass:[Item class]]) {
    ItemCell *itemCell = [self.menuTV dequeueReusableCellWithIdentifier:menuItemCellIdentifier];
    Item *item=(Item *)dic;
    itemCell.headerLabel.text=@"here";
    return itemCell;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理自定义UITableViewCell XIB中的两个视图? - How to handle two views in custom UITableViewCell XIB? 如何在cellForRowAtIndexPath中分配和初始化自定义UITableViewCell NOT? - How can I allocate and initialize a custom UITableViewCell NOT in cellForRowAtIndexPath? 如何在目标c的cellForRowAtIndexPath中返回两种类型的自定义表视图单元格之一 - how to return one of two types of custom table view cells in cellForRowAtIndexPath in objective c 如何在Swift 1.2中的cellForRowAtIndexPath中返回两种类型的自定义表视图单元格之一 - how to return one of two types of custom table view cells in cellForRowAtIndexPath in Swift 1.2 如何在UITableView中使用两个不同的Custom UITableViewCell? - How can I use two different Custom UITableViewCell's in a UITableView? 如何在情节提要中的自定义UITableViewCell中配置UITableView? - How to configure UITableView inside custom UITableViewCell in a Storyboard? 如何只配置一次自定义UITableViewCell? - How to configure custom UITableViewCell just once? 如何使具有多个UITableViewCell的tableView(cellForRowAtIndexPath)可读? - How to make tableView(cellForRowAtIndexPath) with multiple UITableViewCell readable? 为两个自定义UITableViewCell设置不同的高度 - Set different height for two custom UITableViewCell 两个UIViewControllers的不同UITableView中的一个自定义UITableViewCell - One Custom UITableViewCell in different UITableView of two UIViewControllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM