Что-то неизвестное с UITableView - PullRequest
0 голосов
/ 01 февраля 2012

Я создаю пользовательские ячейки табличного представления, но результат неверен. Мой код:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *currentComment = [comments objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"TitleCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    int commentLevel = [[currentComment objectForKey:@"level"] intValue];
    NSString *commentText = [currentComment objectForKey:@"text"];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    [titleLabel setFont:[UIFont fontWithName:@"Verdana" size:17.0]];
    titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    CGSize textSize;
    if (commentLevel == 0) {
        textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(310, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        titleLabel.frame = CGRectMake(5, 5, textSize.width, textSize.height);
    } else {
        textSize = [commentText sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake((305-10*commentLevel), FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        titleLabel.frame = CGRectMake(15 + 10*commentLevel, 5, textSize.width, textSize.height);
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06-arrow-northwest"]];
        img.frame = CGRectMake(5, 5, 15, 15);
        [cell.contentView addSubview:img];
    }
    titleLabel.text = commentText;
    [cell.contentView addSubview:titleLabel];


    return cell;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *currentComment = [comments objectAtIndex:indexPath.row];
    int commentLevel = [[currentComment objectForKey:@"level"] intValue];
    NSString *text = [currentComment objectForKey:@"text"];
    CGSize size;
    if (commentLevel == 0) {
        size = [text sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] constrainedToSize:CGSizeMake(310, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    } else {
        size = [text sizeWithFont:[UIFont fontWithName:@"Verdana" size:17.0] constrainedToSize:CGSizeMake((305-10*commentLevel), FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];

    }
    float height = size.height;
    height = height + 40;
    return height;
}

Результат: Это скриншот, когда загружен uitableview:

enter image description here

но если эта ячейка станет невидимой и после нее видимый результат будет плохим:

enter image description here

Где моя ошибка?

1 Ответ

2 голосов
/ 01 февраля 2012

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

titleLabel.tag = 0xff ;

и

img.tag = 0xfe ;

Затем при повторном использовании ячейки:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:CellIdentifier] autorelease];
} else {
    [[cell viewWithTag:0xff] removeFromSuperview];
    [[cell viewWithTag:0xfe] removeFromSuperview];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...