cell.contentView systemLayoutSizeFittingSize: does not work for dynamic height tableview

廉价感情. 提交于 2019-12-21 04:57:14

问题


I tried to use autolayout in custom uitableviewcell And tried to implement the dynamic height according to this SO topic

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

But my cell is created by xib file. So the method is somewhat different Here is my code in heightForRowAtIndexPath

   -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
 myDataItem *item= [[data items] objectAtIndex:[indexPath row]];



   CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
    if (!cell) {
        cell =  [CustomCell alloc] init];
        [self.offscreenCells setObject:cell forKey:@"CustomCell"];
      }  



    [cell.commentView setText:item.comment]; 
    [cell.displayNameView setText:item.displayNameOfItemOwner];

    [cell.timeAgoView setText:item.timeAgo];
   [cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]];


    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];


    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height+1; }

And the height that I got from [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] is alway "nil" " When I tried to debug ,I found that all subviews of my customcell are "nil" but the cell itself is not "nil". This might because the customCell of mine is made by nib and when I do [[CustomCell alloc] init] the cell is not fully created.

But I did tried to change method to cell = [_tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

The result is still the same. Every subview is nil . This make the height return to be zero and the make my tableView display incorrectly. Are there any way to solve this ?

I tried to make these upon AutoLayout .Actually, in the previous version ,that I didn't use AutoLayout and calculate the cell height manually .It did work perfectly. Anyway, I just want to tried AutoLayout.

Please help .Thanks in advance


回答1:


Here is my solution I alloc new cell by NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

        cell = [topLevelObjects objectAtIndex:0];

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath    *)indexPath
 {
 myDataItem *item= [[data items] objectAtIndex:[indexPath row]];



  CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
if (!cell) {
         NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
 // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

        cell = [topLevelObjects objectAtIndex:0];

    [self.offscreenCells setObject:cell forKey:@"CustomCell"];
  }  



[cell.commentView setText:item.comment]; 
[cell.displayNameView setText:item.displayNameOfItemOwner];

[cell.timeAgoView setText:item.timeAgo];
  [cell.imageView setImage:[UIImage imageNamed:@"image"]];


cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds));

[cell setNeedsLayout];
[cell layoutIfNeeded];


CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height+1; }



回答2:


As far as I know, under iOS 8 the proposed solution is not going to work for cells with multiline labels because systemLayoutSizeFittingSize will not take into account the desired cell width (simply put, it will try to layout the label into a single line, thus making the cell wider than it should).

I managed to solve this issue by installing a width constraint in cell's contentView instead of setting the cell bounds. Remember to uninstall such constraint right after calling systemLayoutSizeFittingSize:.



来源:https://stackoverflow.com/questions/22621586/cell-contentview-systemlayoutsizefittingsize-does-not-work-for-dynamic-height-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!