Reputation: 1776
I'm trying to reduce set a specific tableview cell's height to zero depending on particular conditions. I have used the tableview delegate methods as so:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
return 0;
}
return 45.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Only indicating the necessary code to hide the cell
if (indexPath.row == 1) {
cell.hidden = YES;
}
}
It works as it should , hiding the cell. However, I get a warning which I am weary about incase the app doesn't get approved. Below is the log:
Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x7fb903c3bab0 UILabel:0x7fb903c3afb0'Vehicle Type'.bottom == UITableViewCellContentView:0x7fb903c3ae90.bottomMargin - 4>",
"<NSLayoutConstraint:0x7fb903c3bb50 UILabel:0x7fb903c3afb0'Vehicle Type'.top == UITableViewCellContentView:0x7fb903c3ae90.topMargin + 4>",
"<NSLayoutConstraint:0x7fb903c8c760 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fb903c3ae90(0)]>"
)
NB: I am using a custom tableviewcell
Upvotes: 0
Views: 44
Reputation: 2372
The console output tells me that the cell contains a label, the top and bottom of which are constrained to have a 4 point gap to the cell's content view. When the cell has a height of zero (or anything less than 8 points), the label would need to have a negative height to satisfy those constraints. This isn't allowed, so Autolayout needs to ignore at least one of your constraints. It will start by ignoring the constraint with the lowest priority. However, in your case both the top and bottom constraints have a priority of 1000. Autolayout deals with this ambiguity by breaking one of the constraints and logging the warning.
You can overcome this problem by changing the priority of one of your constraints to 999. You can also test the fix in Interface Builder: adjust the cell's height to zero, and if a red error appears, your constraints are ambiguous at zero height.
I don't believe this would be grounds for AppStore rejection.
Upvotes: 0
Reputation: 2256
OK, if you cannot delete the object from data source for some reason, then I think you need to carefully handle it in your data source methods instead of set the rowHeight
to 0
to "hide the cell". For example, if you want to "hide" the first cell, you probably need to do something like:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count - 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
object = self.dataSource[indexPath.row + 1];
......
return cell;
}
Upvotes: 1