如何使用具有多个自定义单元格的单个TableVIew

时间:2011-11-22 01:08:25

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

我有一个表视图,我想要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;
    }

2 个答案:

答案 0 :(得分: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;
        }
    }

答案 1 :(得分:0)

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];