【问题标题】:UITableView layout not refreshing until scrolledUITableView 布局在滚动之前不会刷新
【发布时间】:2011-05-10 04:55:02
【问题描述】:

我创建了一个 UISplitViewApplication 作为我的新项目。在 potrait 模式下,我有一个按钮,单击该按钮会下拉 UITableView。它看起来如下:

当我单击其中一行时,此 UITableView 被关闭。然后,当我再次单击 Groups 按钮时,呈现的 UITableView 的布局现在变得一团糟:

但是,当我滚动 TableView 以便再次重新加载某些行时,重新加载的行的格式会很好。为什么会这样?我该如何解决?

这是我在索引路径处的行单元格:

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

    MyCell *cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (MyCell *) currentObject;
                break;
            }
        }
        cell.delegate = self;
   // }


    if ([posts count] > 0){
         cell.star.hidden = YES;

        [lazyImages addLazyImageForCell:cell withIndexPath:indexPath];
        cell.title.text = [[posts objectAtIndex:indexPath.row] message];

        cell.detailed.autoresizesSubviews = YES;
        cell.detailed.text = [NSString stringWithFormat:@"%@", [[[posts objectAtIndex:indexPath.row] creator] username]];


        if ([[[posts objectAtIndex:indexPath.row] embeds] count] != 0){
            NSString * URL = [[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] url];
            float height = [[(Embed * )[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] height] floatValue];
            float width =  [[(Embed * )[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] width] floatValue];
            [cell.embed setFrame:CGRectMake(cell.detailed.frame.origin.x, cell.detailed.frame.origin.y + 15, width, height)];
            cell.embed.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL]]];
        }

        if ([[[posts objectAtIndex:indexPath.row] stars] count] != 0){
            cell.star.hidden = NO;
            cell.star_creator.text = [(Login *)[[[[posts objectAtIndex:indexPath.row] stars] objectAtIndex:0] user] username];
        } 

    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText =[[posts objectAtIndex:indexPath.row] message];
    UIFont *cellFont = [UIFont fontWithName:@"Arial" size:14.0];
    CGSize constraintSize = CGSizeMake(600.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    float height = 0.0;

    if ([[[posts objectAtIndex:indexPath.row] embeds] count] != 0)
        height =  [[[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] height] floatValue];

    if (labelSize.height + 20 + height < 48)
        return 55;
    else
        return labelSize.height + height + 48;

}

【问题讨论】:

  • 你的UITableViewCells有动态高度吗?
  • 是的,它具有动态高度。我刚刚上传了该代码
  • 并尝试将CellIdentifier 设为我的(动态)。
  • 实际上在第二次加载那个凌乱的 UITableView 时,它不会再次调用 cellForRowAtIndexPath ......只有当我滚动时它才会调用 cellForRowAtIndexPath,这就是它再次格式化的原因......为什么这是
  • 我遇到了类似的问题,这个解决方案帮助了我-stackoverflow.com/a/17166033/2082569

标签: iphone objective-c ipad uitableview


【解决方案1】:

转自之前的评论:重新加载 viewWillAppear 中的可见单元格。

【讨论】:

    【解决方案2】:

    我以前经历过这个,在你的CellForRowAtIndex委托中,不要检查单元格是否等于nil,继续前进。


    更新

      NSString *sIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sIdentifier];
    
      // don't check if(cell == nil) 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:sIdentifier] autorelease]; 
    

    【讨论】:

    • 如果你不检查它是否为nil,你怎么知道你是否需要创建一个
    • @aherlambangl 我很高兴与您讨论这个问题,但是您是否先尝试过?
    • 是的,我有......但它仍然很乱......我刚刚删除了 if cell == nil 正确并保留循环内的任何内容。我已经编辑了上面的帖子以反映我的 cellforrowatindexpath 代码
    • @aherlambang:如果这不起作用,您能否至少向我提供您正在创建UITableViewCells的代码部分
    • 如果您要每次都创建一个新单元格,为什么还要让单元格出列呢?而且你会使用大量的内存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多