First, the system automatically removes the proper constraint so everything is working as expected but I would like to understand how to make my code better. Uitableview is working in iOS 8.0 automatic mode and does not implement heightforindexpath method.
The uitableview displays images in uitableviewcells and I am just trying to add a ratio constraint to the uiimageview so that the image is displayed correctly. It seems it is working as expected but there might be something that I don't understand (order of method called or constraint not given at the right moment I am not sure).
Below is part of the code you can have a look at. Image width and height are stored in database and used to set up the constraint before even downloading the image.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierPostImage];
if (cell == nil) {
cell = [[PostImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierPostImage]; }
// Adding image
[cell.imagePost setImageWithURL:[NSURL URLWithString:[[self.myData objectAtIndex:indexPath.row] valueForKey:@"image"]] placeholderImage:[UIImage imageNamed:@""]];
// Adding constraint
[cell setConstraints:[[[self.myData objectAtIndex:indexPath.row] valueForKey:@"imageWidth"] doubleValue] withHeight:[[[self.myData objectAtIndex:indexPath.row] valueForKey:@"imageHeight"] doubleValue]];
return cell;
}
The setConstraints method of the PostImageTableViewCell custom class is here. What I am trying to achieve is remove the old constraint given to the previous cell and add new one for new image in next cell ... but from what I understand from the logs it seems it does not do that. Is it mixing constraints or do I have to give a ratio number cleaner?
- (void)setConstraints:(double)width withHeight:(double)height {
int fullWidth = [[UIScreen mainScreen] applicationFrame].size.width;
[self.imagePost removeConstraint:self.imageConstraint];
self.imageConstraint = [NSLayoutConstraint constraintWithItem:self.imagePost attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.imagePost attribute:NSLayoutAttributeHeight multiplier:(MAX(width,(fullWidth-20))/height) constant:0.0];
[self.imagePost addConstraint:self.imageConstraint];
}
Error message. It seems it does not like the ratio constraint given ... Again, it displays correctly in the simulator/device but I'd like to understand what I am doing wrong.
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fb80cbac720 V:|-(11.5)-[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39'] (Names: '|':UITableViewCellContentView:0x7fb80cb8bfd0 )>",
"<NSLayoutConstraint:0x7fb80cbac7c0 V:[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39'(23)]>",
"<NSLayoutConstraint:0x7fb80cbac580 V:[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39']-(11.5)-[CustLabelWithPadding:0x7fb80b90d8e0'Tr']>",
"<NSLayoutConstraint:0x7fb80cbac8c0 V:[CustLabelWithPadding:0x7fb80b90d8e0'Tr'(>=34.5)]>",
"<NSLayoutConstraint:0x7fb80cbac840 V:[CustLabelWithPadding:0x7fb80b90d8e0'Tr']-(1)-[CustLabelWithPadding:0x7fb80cba83e0'Rr']>",
"<NSLayoutConstraint:0x7fb80cbac9e0 V:[CustLabelWithPadding:0x7fb80cba83e0'Rr'(>=34.5)]>",
"<NSLayoutConstraint:0x7fb80cbac910 V:[CustLabelWithPadding:0x7fb80cba83e0'Rr']-(1)-[UIImageView:0x7fb80cba8a20]>",
"<NSLayoutConstraint:0x7fb80cbaca30 V:[UIImageView:0x7fb80cba8a20]-(1)-[UIView:0x7fb80cba8c20]>",
"<NSLayoutConstraint:0x7fb80cbacc00 V:[UIView:0x7fb80cba8c20(50.6)]>",
"<NSLayoutConstraint:0x7fb80cbacb80 V:[UIView:0x7fb80cba8c20]-(11.5)-| (Names: '|':UITableViewCellContentView:0x7fb80cb8bfd0 )>",
"<NSLayoutConstraint:0x7fb80cbb0130 H:[UIImageView:0x7fb80cba8a20(352)]>",
"<NSLayoutConstraint:0x7fb80cbb2080 UIImageView:0x7fb80cba8a20.width == 1.65647*UIImageView:0x7fb80cba8a20.height>",
"<NSLayoutConstraint:0x7fb80cbabf60 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fb80cb8bfd0(392.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb80cbb2080 UIImageView:0x7fb80cba8a20.width == 1.65647*UIImageView:0x7fb80cba8a20.height>
I cleaned the code a bit but basically width available is 375-20*1.15. Image height is 212.5 (number stored in database) so final ratio would be 352/212.5 = 1.65647... as seen in error message. The other constraint are working fine I think as they are used for cell without images and I have no error message for them.