Hi I'm able to achieve auto-resizing table view cells (based on amount of text) by pinning textLabel's top, bottom, left and right to content view's top, bottom, left and right. I was able to get different cell height for cell with different text length. But I want to achieve same auto-resizing result with button added as subview to cell. I have added exact same contrains to button as I have it with textLabel. Any help is greatly appreciate .
Asked
Active
Viewed 780 times
3
-
What you are trying to add on button? Multiline text or anything else – Gopesh Gupta May 07 '16 at 10:20
-
There is nothing added as subview over button.I just want to show text on cell. I just want my cell to resize depending on text length of button.titleLabel. – Jaffer Sheriff May 07 '16 at 10:22
-
I think you have the easier solution will be to just use a textlabel and resize the cell like you have explained in question and drag a button and add constraints relative to label(which means button will have same frame as label) – Ahmad Ishfaq May 07 '16 at 11:51
-
Yeah that will work. But I'm just curious to know why constains don't exactly work for UIButton like the way they do for UILabel inside UITableViewCell. I would appreciate any solution that make UIButton to resize cell just like UILabel does. – Jaffer Sheriff May 07 '16 at 12:04
1 Answers
0
Summary : Here is an hand-made example about this problem.
First, read the answer about autoresizing tableviewcell with UILabel because you will do like that with a little changes. The difference between UILabel and UIButton is UILabel has Intrinsic Content Size based on Width and Height. So you need to subclass UIButton and override method :
-(CGSize)intrinsicContentSize {
return CGSizeMake(self.frame.size.width, self.titleLabel.frame.size.height);
}
In your CustomCell class, override method
- (void)layoutSubviews{
[super layoutSubviews]; //btnTest is your Button
[self.contentView setNeedsLayout];
self.btnTest.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.btnTest.titleLabel.frame);
}
In your ViewController :
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath{
if ([cell isKindOfClass:[TestCell class]]) {
TestCell *testCell = (TestCell *)cell;
[testCell.btnTest setTitle:@"An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie." forState:UIControlStateNormal];
#warning You need to have this below line, to calculate height when the first time viewDidLoad.
testCell.btnTest.titleLabel.text = @"An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.";
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
//
self.prototypeCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
[self.prototypeCell setNeedsLayout];
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height +1;
}
-
In my case I cannot return a 'CGFloat' value in 'heightForRowAtIndexPath' because each row is unique and has different subviews. I tried what you suggested and its not working. – Jaffer Sheriff May 08 '16 at 12:59
-
Moreover, Consider a scenario in which a simple view contains UIButton as subview. I pinned top, left , right and bottom of UIButton to its superview. I want my view to resize its size depending on text of UIButton. Even this simple thing is not achievable. – Jaffer Sheriff May 08 '16 at 13:34
-
@JafferSheriff : I dont know what you means that you cannot return 'CGFloat' ????? Because you will need 'float' value to determine all row height. Yes, have you downloaded my example and see the row height changing based on text in UIButton ? – Martin Le May 08 '16 at 17:04
-
What I meant was I cannot use heightForRowAtIndexPath to return a value just like the way you did. As per apple's suggession we don't have to use heightForRowAtIndexPath method to return a float value for auto-resizing to work. [Refer] (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html) . Your example works fine and I just have a simple question that how can you make cell to autoresize without having to return a value in heightForRowAtIndexPath ? – Jaffer Sheriff May 09 '16 at 05:38
-
@JafferSheriff No, you can do that.If cell resizing based on Button, you return like me do ,others cell you only need return `UITableViewAutomaticDimension` in heightForRowAtIndexPath. – Martin Le May 09 '16 at 08:57
-
@JafferSheriff and I also update my Example in Github, please check it :D If it is true, please finish this thread ;) – Martin Le May 09 '16 at 09:03