【问题标题】:How to have a single TableVIew with Multiple Custom Cells如何拥有一个带有多个自定义单元格的 TableVIew
【发布时间】:2011-11-22 01:08:25
【问题描述】:

我有一个表格视图,我想要其中包含 3 个不同的自定义单元格,现在这段代码只有一个。

如何修改我的代码,使案例 2 和案例 9 都有自己的自定义表格单元格和自己的单元格标识符?请记住,这段代码在 if/else 语句中,因为我的 VC 中有 2 个不同的表视图。

static NSString *CellIdentifier = nil;

    if (tableView == self.mytableview)
    {
    }
    else if (tableView == self.vitalsTableView)
    {
            CellIdentifier = @"textCell";
            VitalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            switch (indexPath.row) {
                case 0:
                    cell.vitalsLabel.text = @"Temperature";
                    break;
                case 1:
                    cell.vitalsLabel.text = @"Pulse";
                    break;
                case 2:
                    cell.vitalsLabel.text = @"Blood Pressure";
                    break;
                case 3:
                    cell.vitalsLabel.text = @"Respiratory Rate";
                    break;
                case 9:
                    cell.vitalsLabel.text = @"Smoking Status";
                    break;
                default:
                    break;
            }
            return cell;
    }

【问题讨论】:

  • 你想要不同的 UITableViewCell styleTypes 还是想要继承 UITableViewCell 并创建自己的?

标签: iphone objective-c cocoa-touch uitableview uitextfield


【解决方案1】:

这就像你已经拥有的东西,但对你修改的类和标识符进行了小幅修改,我希望我得到了你的要求。

static NSString *CellIdentifier = nil;

if (tableView == self.mytableview)
{
}
else if (tableView == self.vitalsTableView)
{
    if ([indexPath row] == 2 || [indexPath row] == 9) {
        CellIdentifier = @"Cell2Or9";
        Cell2or9 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        switch (indexPath.row) {
            case 2:
                cell.vitalsLabel.text = @"Blood Pressure";
                break;
            case 9:
                cell.vitalsLabel.text = @"Smoking Status";
                break;
            default:
                break;
        }
    }
    else{
        CellIdentifier = @"CellNot2Or9";
        CellNot2or9 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        switch (indexPath.row) {
            case 0:
                cell.vitalsLabel.text = @"Temperature";
                break;
            case 1:
                cell.vitalsLabel.text = @"Pulse";
                break;
            case 3:
                cell.vitalsLabel.text = @"Respiratory Rate";
                break;
            default:
                break;
        }
    }

【讨论】:

  • 如果我不知道具有自定义单元格类型 2 的行的索引路径怎么办?我可以使用其他条件代替 if ([indexPath row] == 2 || [indexPath row] == 9) 吗?
  • 这里有更多我想要做的细节:- if ([string1 isequalsToString @"silver"]){cellIdentifier = @"cell1"} else if ([string1 isequalsToString @"gold"]) {cellIdentifier = @"cell2"} 而不是 if ([indexPath row] == 2 || [indexPath row] == 9) ?
  • 您应该能够根据您想要的任何条件执行此操作。指数通常是有意义的,但我可以看到很多其他用例。
  • 太好了,我试过了,它可以工作,我通过故事板使用静态自定义单元格并为每个自定义单元格设置单元格标识符,我可以根据任何条件更改或识别单元格。将在下面的答案中发布我的代码
【解决方案2】:
static NSString *cellIdentifier;

     NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];

     if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
     {
         cellIdentifier = @"cell";
     }
     else if ([membershipType isEqualToString:@"platinum"])
     {
         cellIdentifier = @"premiumCustomCell";
         cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
     }

     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     if (!cell) {
         cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row];  

【讨论】:

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