Отображать несколько пользовательских ячеек в UITableView? - PullRequest
2 голосов
/ 04 марта 2012

Я использую Xcode 4.2 на SnowLeopard, и мой проект использует раскадровки.Я пытаюсь реализовать UITableView с 2 различными типами ячеек, sessionCell и infoCell.Я могу заставить 2 типа появляться в одном и том же списке, но теперь у меня возникла новая проблема ?!sessionCell отображается один раз, а затем после него отображается число X infoCells - как я и хотел - за исключением того, что первое infoCell всегда перезаписывается на sessionCell!

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.people count];
}

//inside cellForRowAtIndexPath
    if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
}
...
return cell;

Я пытался сказать return array count + 1, или даже жестко закодированный return 7 (это соответствует моему примеру), но оба неверны!

myObject *person = [self.people objectAtIndex:indexPath.row];

Или моя проблема заключается в приведенной выше строке?Я даже пытался indexPath.row+1 ...

Любая помощь будет принята с благодарностью !!

Ответы [ 2 ]

3 голосов
/ 04 марта 2012

Если я правильно понимаю ваш вопрос, первая infoCell (вторая UITableView строка) должна отображать данные объекта от первого лица, верно?

Тогда кажется, что вы хотите:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sessionCellID = @"sessionID";
    static NSString *infoCellID = @"infoID";

    if( indexPath.row == 0 ) {
        SessionCellClass *cell = nil;
        cell = (SessionCellClass *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
        if( !cell ) {
            //  do something to create a new instance of cell
            //  either alloc/initWithStyle or load via UINib
        }
        //  populate the cell with session model
        return cell;
    }
    else {
        InfoCellClass *cell = nil;
        cell = (InfoCellClass *)[tableView dequeueReusableCellWithIdentifier:infoCellID];
        if( !cell ) {
            //  do something to create a new instance of info cell
            //  either alloc/initWithStyle or load via UINib
            // ...

            //  get the model object:
            myObject *person = [[self people] objectAtIndex:indexPath.row - 1];

            //  populate the cell with that model object
            //  ...
            return cell;
        }
    }

и вам нужно вернуть [[self people] count] + 1 для подсчета строк:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self people] count] + 1;
}

, чтобы n-я строкапоказывает (n-1) -й данные.

0 голосов
/ 04 марта 2012

если вы посмотрите на раздел if else, он показывает, что первая строка - "sessionCell", а все остальные строки - "infoCells"и все остальные ряды peopleCells

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.people count] + 2; // Added two for sessionCell & infoCell
}

//inside cellForRowAtIndexPath
if(indexPath.row == 0) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
} else if (indexPath.row == 1 {
    cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"];
    Person *person = [self.people objectAtIndex:index.path.row - 2];
}

...
return cell;

Еще лучше, я бы попробовал сделать два разных раздела продажи, один для информации и один для людей

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section == 0 ? 2 : self.people.count
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell = nil;
    if (indexPath.section == 0) {
        if(indexPath.row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
            // configure session cell
        } else if (indexPath.row == 1 {
            cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
            // configure info cell
        }
    } else {
         cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
         Person *person = [self.people objectAtIndexPath:indexPath.row];
         // configure person cell
    }

    return cell;
}
...