0

I have a table with 3 sections, all three sections have different number of rows. The cell values should populate from different array's. So how should i find out which section is it in method cellForRowAtIndexPath:???

Please help!!!

Code:

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
    currentSection = section;
    int number = 0;
    switch (section) {
        case 0:
            number = self.basic.count;
            break;
        case 1:
            number = allSectors.count;
            break;
        case 2:
            number = 1;
            break;
        default:
            number = 0;
            break;
    }

    return number;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];

    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    CGFloat fontSize = [UIFont systemFontSize];
    CGFloat smallFontSize = [UIFont smallSystemFontSize];
    switch (indexPath.section) {
    case 0:
        cell.textLabel.text = [self.basic objectAtIndex:indexPath.row];
        break;
    case 1:
        cell.textLabel.text = [self.allSectors objectAtIndex:indexPath.row];
        break;
    case 2:
        cell.textLabel.text = @"None";
        break;

    default:
        break;
}


return cell;
}

1 Answer 1

1

Just use indexPath.section to get the table section, and indexPath.row to get the row in that section. These properties are defined in the category NSIndexPath (UIKitAdditions).

Sign up to request clarification or add additional context in comments.

3 Comments

ok cool. I used this and i suppose its working but when i am trying to scroll down and see what's get displayed in the next section , it crashes. Is there any thing wrong i am doing. Please see above for code.
@Ashutosh, I don't see anything that's obviously wrong. It'd help to have the error and the line where the crash happens, but perhaps this is straying into new question territory.
Sorry...my fault. allSectors is an NSMutableArray which has NSSet inside it. Fix: cell.textLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@"SECTORNAME"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.