【问题标题】:How to configure cellForRowAtIndexPath to handle two different custom UITableViewCell types如何配置 cellForRowAtIndexPath 以处理两种不同的自定义 UITableViewCell 类型
【发布时间】:2014-08-19 03:35:30
【问题描述】:

我正在尝试使用两个不同的自定义 UITableViewCell,但我正在为如何完成这项工作而苦苦挣扎。我有以下内容,但认为一切都需要称为单元格。另外,我不确定我的返回类型是否正确。设置它的规范方法是什么?

这是this question 的后续行动(虽然不完全相关)。

-(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;
  }

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    假设您在 Storyboard 中使用原型单元格,或者您已经注册了单元格标识符,那么您的代码几乎是正确的。您应该只将您需要的单元格类型出列 -

    -(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;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多