Ruth85
Ruth85

Reputation: 745

Apply different cell properties depending on section

following code works properly but it applies same cell text to all sections. How could I apply selection case also depending on section? Thank you.

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

Upvotes: 0

Views: 102

Answers (2)

Tendulkar
Tendulkar

Reputation: 5540

You have to check for the indexPath.section and also indexPath.row:

if (indexPath.section == 0) {

    if (indexPath.row) {

        ...
    }

} else if (indexPath.section == 1)

    if (indexPath.row) {

        ...
    }

} else if ...

Upvotes: 0

jtbandes
jtbandes

Reputation: 118691

You can use indexPath.section to get the section number, just like how you're using indexPath.row for the row number.

Upvotes: 1

Related Questions