简体   繁体   English

UITableView自定义单元显示奇怪的结果?

[英]UITableView custom cell showing weird results?

UITableView custom cell showing weird results? UITableView自定义单元显示奇怪的结果? I have my code below. 我下面有我的代码。 Everything was showing fine until I put enough data to go off the initial screen then it started going crazy? 一切都很好,直到我放入足够的数据以离开初始屏幕,然后开始变得疯狂? My custom cell is shown below. 我的自定义单元格如下所示。

在此处输入图片说明

// 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; 
}

You queing up old cells as they go off screen do to your dequeReusableCell , and the content and properties from the old cell must still be there. 您可以在旧单元格离开屏幕时对其进行dequeReusableCell ,以对dequeReusableCell ,并且旧单元格的内容和属性必须仍然存在。 When you add content to a cell make sure to clear any previous properties so you can avoid these issues, or just turn off dequeReusableCell . 将内容添加到单元格时,请确保清除所有先前的属性,以便避免这些问题,或者仅关闭dequeReusableCell

It looks like a cell-reuse problem. 看起来像是单元重用问题。 Why dont your turn off the dequeue-cell mechanism and see if the problem goes away. 为什么不关闭出队单元机制,看看问题是否消失了。 If it does, take a look at the code in your custom cell - are you creating multiple copies of the field labels? 如果是这样,请查看自定义单元中的代码-您是否要创建字段标签的多个副本? It looks that way. 看起来就是那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM