iPhone - UITableView для отображения двух разных пользовательских ячеек - PullRequest
1 голос
/ 07 апреля 2011

В моем UITableView я хочу показать индикатор загрузки в последней ячейке.Для оставшихся ячеек я создал настраиваемую ячейку табличного представления, которая работает нормально.Но когда я прокручиваю до конца, он вылетает и не может загрузить индикатор загрузки (это другой пользовательский вид ячейки).Вот мой код.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"HomeViewCell";

    UITableViewCell* cellToReturn = nil;

    // Configure the cell.
    NSInteger row = [indexPath row];
    if (row < [jokesTableArray count])
    {
        CustomTableCell* cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        NSDictionary* dict = [jokesTableArray objectAtIndex:indexPath.row];
        NSNumber* rating = [[ServerCommunicator getInstance] getCurrentUserJokeRating:[dict objectForKey:@"id"]];
        cell.userRatedTheJoke = rating != nil ? YES : NO;
        cell.titleLabel.text = [dict objectForKey:@"title"];
        cell.descriptionLabel.text = [dict objectForKey:@"text"]; 
        cell.upVotes.text = [NSString stringWithFormat:@"%2.2f",[[dict objectForKey:@"rating_count"] floatValue]];
        [cell setRating:rating != nil ? [rating intValue] : [[dict objectForKey:@"rating_count"] intValue]];
        NSString* authorName = [dict objectForKey:@"author"];
        if (authorName != nil && ![authorName isEqual:[NSNull null]]) 
        {
            cell.author.text = [NSString stringWithFormat:@"By:%@",authorName];     
        }
        cellToReturn = cell;
    } 
    else 
    {
        KMLoadingIndicatorCell* cell = (KMLoadingIndicatorCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            KMLoadingIndicatorCell* cell = [[[KMLoadingIndicatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.loadingLabel.text = @"Loading...";
            cellToReturn = cell;
        }
    }

    return cellToReturn;
}

Ответы [ 2 ]

1 голос
/ 07 апреля 2011

Так как вы используете тот же reuseIdentifier, строка

KMLoadingIndicatorCell* cell = (KMLoadingIndicatorCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

- это, скорее всего, cell для экземпляра CustomTableCell, который теперь используется повторно, а не ожидаемый KMLoadingIndicatorCell. Вы должны использовать разные идентификаторы для двух разных типов ячеек.

0 голосов
/ 07 апреля 2011

Вы должны использовать другой идентификатор ячейки для своей пользовательской ячейки.

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