开发者

How to display different element from different array in different section in single table view?

开发者 https://www.devze.com 2023-03-18 23:12 出处:网络
I have a table with 3 sections, all three sections have different number of rows. The cell values should populate from different array\'s.

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 =开发者_如何学Go @"None";
        break;

    default:
        break;
}


return cell;
}


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消