简体   繁体   中英

custom UITableviewcell height not set correctly

I explored the existing Q&As on SO about this question but did not find my answer.

I know this is caused by tableview not knowing the height of the custom cell at run time,but not sure how to get over this. This is iOS 8 + Xcode 6. I did all the auto layout required methods for intrinsic size of the custom cell...

  • Standard tableview with standard cells, only one (row = 2) created in code as custom cell;

    customCell:

     -(CGSize)intrinsicContentSize

    { //hardcode for testing purposes

    return CGSizeMake(500.0, 450.0);

    }

  • when running on iOS simulator, found that the customCell is displayed "underneath" other cells below its row, with height standard, the same way other standard cell height is set, instead of its height set much bigger than other cells.

In table view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    pageViewCellTableViewCell* customCell;

    if (indexPath.row != 2)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:@"regularCell" forIndexPath:indexPath];


        cell.textLabel.text = [NSString stringWithFormat:@"Table Row %lu", indexPath.row];
        return cell;
    }
    else
    {
        customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell"] ;

        if (customCell == nil) {

            customCell = [[customCell alloc] init];

        }

        customCell.parentViewController = self;


        [customCell  setNeedsUpdateConstraints];
        [customCell setNeedsLayout];
        [customCell setNeedsDisplay];


        return customCell;

    }

    return nil ;
}

- (CGFloat)tableView: (UITableView*)tableView EstimatedHeightForRowAtIndexPath: (NSIndexPath*) indexPath
 {
     if (indexPath.row != 2)
         return 10;
     else
         return 450;
 }

 - (CGFloat)tableView: (UITableView*)tableView HeightForRowAtIndexPath: (NSIndexPath*) indexPath
 {
     if (indexPath.row != 2)
         return 10;
     else
         return 450;
 }

When running on simulator:

Got the following Error msg: Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

模拟器截图,见第 2 行被塞在其他行下<code>在此处输入代码</code>

I faced a similar error couple of days ago. I suggest you to check the Auto Layout constraints once more.

When setting the tableView.rowHeight = UITableViewAutomatic you have to make sure that each element in the cell has a leading,top & bottom constraints. Else swift can't change the height dynamically based on content size.

You can use self-sizing cells in iOS 8 (Swift code):

tableView.estimatedRowHeight = 88.0
tableView.rowHeight = UITableViewAutomaticDimension

Two tutorials list below:

  1. Understanding Self Sizing Cells and Dynamic Type in iOS 8
  2. iOS 8: Self Sizing Table View Cells with Dynamic Type

Figured it out.

According to the WWDC 2014 video "What's new for Tableview and Collection View":

  1. added constraints from self.view to customTableViewCell.contentView
  2. set intrinsicSize to self.bounds.size

I just dealt with this as well. I'm doing a custom tableViewCell from a nib file instead of using the storyboard cell. I added the code (Objective-C) as suggested above:

self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setEstimatedRowHeight:85.0];

Didn't help. What did help was fixing my constraints. I assumed that by pinning to the top, and then just setting the vertical spacing between the next two objects would be enough. I had also set a fixed height constraint for the UILabels thinking that would help. It didn't. Removing any fixed-height constraints and pinning the bottom object to the bottom of the container resolved the issue for me. Turns out that auto-layout is smarter than me. Who knew!? lol

I was struggling with the UITableViewCell getting compressed vertically using 2 Labels.

Setting bottom spacing to the container as mentioned by Mike C. - did the trick as well for me.

Right click and dragged from the left side of the label bounding box (outer label bounding box, not the label name itself) to the lower left of the Content View opens the Constraint view. Selected "Bottom Space To Container" - Voilà.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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