Uitableviewautomaticdimension Not Working on iOS 8

UITableViewAutomaticDimension not working on iOS 8

add following code in "cellForRowAtIndexPath" method before return cell.

    cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

UITableViewAutomaticDimension not working for footer in iOS8

After test, here is my results

For iOS8, UITableViewAutomaticDimension only work for section header and tableview cell and not work for section footer

BUT strangely, if my tableview use both section header and footer, the UITableViewAutomaticDimension will work for footer.

Then here is the solution

- (void)viewDidLoad {
// I set UITableViewAutomaticDimension for both footer and header
self.tableview.estimatedSectionHeaderHeight = 0; // it should be 0 because header is empty
self.tableview.sectionHeaderHeight = UITableViewAutomaticDimension;

self.tableview.estimatedSectionFooterHeight = 140;
self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;
}
// I return empty view for header
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

For iOS9 and above, UITableViewAutomaticDimension work correctly

Sample Image

UITableViewAutomaticDimension not working with custom table cell

Automatic height works on calculated cell's contentView height. You therefore have to add constraints that will be used to calculate its real height.

Try this:

class UINoteTabelViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}

func fill(_ note: Note) {
let view = UINote(withNote: note, atPoint: .zero)
self.contentView.addSubview(view)

// this will make the difference to calculate it based on view's size:
view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
}

Also, estimatedRowHeight should be set to a specific value that approximately estimates the size of all cells. But that would not be a problem.

why UITableViewAutomaticDimension not working?

In order to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view. In your case you will need to add the missing bottom space to superview constraint for label under the title

UITableViewAutomaticDimension not working until scroll

I ran into the same issue and found out that the accessory cell.accessoryType messes with this automatic resizing when it is not None, so it seems like a bug in Xcode at the moment.

But as @Blankarsch mentioned, calling reloadSections(..) helps to fix this issue if you need to have an accessory.

Accessory to "None" solved my issue



Related Topics



Leave a reply



Submit