Пользовательская ячейка UITableView, показывающая странные результаты? - PullRequest
1 голос
/ 01 марта 2011

UITableView пользовательская ячейка показывает странные результаты?У меня есть мой код ниже.Все показывалось нормально, пока я не положил достаточно данных, чтобы уйти с начального экрана, затем он начал сходить с ума?Моя пользовательская ячейка показана ниже.

enter image description here

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"customCell"; // This is identifier given in IB jason set this.

    PostTableCustomCellController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSLog(@"Cell created");

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"PostTableCustomCellController" owner:nil options:nil];

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass:[PostTableCustomCellController class]])
            {
                cell = (PostTableCustomCellController *)currentObject;  
            }
        }
    }

    Post *post = [postsArray objectAtIndex:indexPath.row];

    cell.authorName.text = post.author;
    cell.deadline.text = post.deadline;
    cell.description.text = post.description;


    Post *myPost = [postsArray objectAtIndex:[indexPath row]];
    NSString *text = myPost.description;

    // Configure the cell...            
    // [[cell authorName] setText:@"1 day"];
    // [[cell distance] setText:@"Austin, TX"];
    // [[cell description] setText:@"dd" ];


    // Might can remove these
    UILabel *locationLabel = (UILabel *) [cell distance];
    UILabel *postTextView = (UILabel *) [cell description];



    //CGSize maximumLabelSize = CGSizeMake(254,88.0f);



    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 35.0f);

    CGFloat realHeight = height + 36.0f - (10); 


    CGSize expectedLabelSize = [text sizeWithFont:postTextView.font 
                                constrainedToSize:constraint 
                                    lineBreakMode:postTextView.lineBreakMode];  
    CGRect newFrame = postTextView.frame;
    newFrame.size.height = expectedLabelSize.height;
    postTextView.frame = newFrame;

    [[cell description] sizeToFit];

    CGRect locationRect = locationLabel.frame; // calls the getter
    locationRect.origin.y = realHeight;

    /* CGFloat locRectHeight = postTextView.bounds.size.height; */
    /* locationRect.origin.y = cell.bounds.size.height; */
    locationLabel.frame = locationRect;

    //[[cell authorName] setText:@"jgervin"];
    [[cell viewForBackground] sizeToFit];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    Post *myPost = [postsArray objectAtIndex:[indexPath row]];
    NSString *text = myPost.description;

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 35.0f);

    return height + (CELL_CONTENT_MARGIN * 2) + 36.0f; 
}

Ответы [ 2 ]

1 голос
/ 01 марта 2011

Вы ставите старые ячейки в очередь, когда они выходят за пределы экрана, к вашему dequeReusableCell, и содержимое и свойства старой ячейки все еще должны быть там.При добавлении содержимого в ячейку обязательно удалите все предыдущие свойства, чтобы избежать этих проблем, или просто отключите dequeReusableCell.

0 голосов
/ 01 марта 2011

Похоже, проблема повторного использования клетки.Почему бы вам не отключить механизм ячейки dequeue и посмотреть, исчезнет ли проблема.Если это так, взгляните на код в пользовательской ячейке - вы создаете несколько копий меток полей?Это выглядит так.

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