UITableView布局不刷新,直到滚动

问题描述:

我创建了一个UISplitViewApplication作为我的新项目。在potrait模式下,我有一个按钮,点击后会下拉UITableView。它看起来如下所示:UITableView布局不刷新,直到滚动

enter image description here

当我点击了行之一,这是的UITableView驳回。后来,当我再次点击组按钮,呈现的UITableView的布局,现在所有的凌乱:

enter image description here

然而,当我滚动的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; 

} 
+0

是你的'UITableViewCells'有动态高度吗? – 2011-05-10 13:52:22

+0

是的,它有动态的高度..我刚刚上传了这个代码 – aherlambang 2011-05-10 13:55:10

+0

,并且还尝试使'CellIdentifier'成为我的(动态)。 – 2011-05-10 13:57:30

转贴:重装可见细胞viewWillAppear中。

我经历过在此之前,在你CellForRowAtIndex委托,如果电池等于零只是不停不检查。


更新从之前的评论

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]; 
+0

好,如果你不检查它是否是零或者不是你怎么知道你是否需要创建一个或不是 – aherlambang 2011-05-10 13:38:03

+0

@aherlambangl我很乐意与您讨论,**但是**您是否先尝试过? – 2011-05-10 13:42:17

+0

是的,我有......并且它仍然凌乱......我只是删除了if cell == nil,并保留循环内的任何内容。我上面编辑了我的帖子,以反映我的cellforrowatindexpath代码 – aherlambang 2011-05-10 13:46:10