So I have looked over this answer. But I am still not getting the heights to return correctly for iOS7. I have a table view with a cell that I laid out in autolayout that is fairly complicated. Whenever I try to override the heightForRowAtIndex
(to make it iOS7 compatible) I get row heights that don't seem to be calculated correctly. When I don't override heightForRowAtIndex
everything works great, except that it doesn't work on iOS7. I am also trying to layout the whole thing automatically using xibs. There isn't really any logic in my DipticCell.m. It's all in the UITableView subclass.
-(id)init{
//Implementation details go BELOW
self.dataSource = self;
self.delegate = self;
[self registerNib:[UINib nibWithNibName:@"DipticCell" bundle:nil] forCellReuseIdentifier:@"dipticCell"];
[self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
self.rowHeight = UITableViewAutomaticDimension;
offScreenCells = [[NSMutableDictionary alloc] init];
return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *reuseIdentifier = @"dipticCell";
if (indexPath.row > 0){
reuseIdentifier = @"cell2";
}
UITableViewCell *cell = [offScreenCells objectForKey:reuseIdentifier];
if (!cell) {
cell = [self dequeueReusableCellWithIdentifier:reuseIdentifier];
[offScreenCells setObject:cell forKey:reuseIdentifier];
}
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
height += 1.0f;
return height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0){
DipticCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dipticCell"];
if (cell == nil) {
cell = [[DipticCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dipticCell"];
}
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"];
}
cell.textLabel.text = @"one";
return cell;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
Just to confirm that everything is in the contentView.
Also, when I override the heightForRowAtIndexPath
I get an "Unable to simultaneously satisfy constraints." warning. But when I don't override that then things seem to work (which makes sense because the cell should just adapt to the height of the content and now I'm trying to override it with a height of 1.)
Here is a link to the project if you want to run it. AutoLayoutCells