Как отобразить разные элементы из разных массивов в разных разделах в едином табличном представлении? - PullRequest
0 голосов
/ 08 июля 2011

У меня есть таблица с 3 разделами, все три раздела имеют разное количество строк. Значения ячеек должны заполняться из разных массивов. Так, как я должен узнать, какой раздел это в методе cellForRowAtIndexPath: ???

Пожалуйста, помогите !!!

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 Ответ

1 голос
/ 08 июля 2011

Просто используйте indexPath.section, чтобы получить раздел таблицы, и indexPath.row, чтобы получить строку в этом разделе.Эти свойства определены в категории NSIndexPath (UIKitAdditions).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...