开发者

UITableView layout not refreshing until scrolled

开发者 https://www.devze.com 2023-03-03 21:40 出处:网络
I have created a UISplitViewApplication as my new project. In the potrait mode I have a button that when clicked will drop down a UITableView. It looks something as the following:

I have created a UISplitViewApplication as my new project. In the potrait mode I have a button that when clicked will drop down a UITableView. It looks something as the following:

UITableView layout not refreshing until scrolled

When I clicked on one of the rows, this UITableView is dismissed. Then when I click again on the Groups button, the layout of the UITableView presented is now all messy:

UITableView layout not refreshing until scrolled

However, when I scroll the TableView so that some of the rows are reloaded again, those that are reloaded are then formatted fine. Why is this and how do I fix this?

Here's my cell for row at index path:

- (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 imageW开发者_开发知识库ithData:[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;

}


Reposted from a previous comment: reload the visible cells in viewWillAppear.


I have been through this before, In your CellForRowAtIndex delegate and don't check if the cell equals nil just keep going.


Update

  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

精彩评论

暂无评论...
验证码 换一张
取 消