根据部分应用不同的单元属性

时间:2011-08-23 08:37:06

标签: iphone objective-c uitableview

以下代码正常工作,但它将相同的单元格文本应用于所有部分。我如何根据部分申请选择案例?谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;         
    }

    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"English";
            break;
        case 1:
            cell.textLabel.text = @"Chinese";
            break;
        case 2:
            cell.textLabel.text = @"Japanese";
            break;
        default:
            break;
    }

    return cell;
}

2 个答案:

答案 0 :(得分:1)

您可以使用indexPath.section获取部分编号,就像您使用indexPath.row作为行号一样。

答案 1 :(得分:0)

您必须检查indexPath.section以及indexPath.row

if (indexPath.section == 0) {

    if (indexPath.row) {

        ...
    }

} else if (indexPath.section == 1)

    if (indexPath.row) {

        ...
    }

} else if ...