Reputation: 341
I have a table view filled by some custom table view cells, in every cell there is a UILabel
whose original numberOFLines
is 5. The contents of this UILabel
could be more than 5 lines so if I set numberOFLines
to 0, this UILabel
could expand to 10 lines or even more so when I set numberOFLines
to 0, cell height should be bigger.
Suppose that at first when numberOfLines
is 5, height of one cell is 200. When I tap this label, numberOfLines
is set to 0, this label tries to display all content and there should be 10 lines of text, height of this cell should be 250 so it can show all contents properly. And when I tap it again, numberOfLines
should go back to 5 and cell height should go back to 200.
So I add a UITapGestureRecognizer
to this label and when tapped, a method is called to reset numberOfLines
:
- (void)mainContentLabelTapped {
NSLog(@"main content label tapped");
(self.mainContentLabel.numberOfLines == 0) ?
[self.mainContentLabel setNumberOfLines:5]:
[self.mainContentLabel setNumberOfLines:0];
}
The problem is that when I tap mainContentLable
, this label itself would expand to more lines or shrink back to 5 lines but cell height remain unchanged until I scroll this cell out of screen and scroll it back again so its height could be recalculated.
So how to change cell height when I click that label?
There are tons of questions in stackoverflow about dynamic height of table view cell but all solutions are to send message to tableview such as begin/endUpdate
or reloadData
. In my question tap gesture is token care in .m
file of my own cell so I can't find a way to send those messages to table view and ask it recalculate cell height.
Thank you!
Upvotes: 1
Views: 829
Reputation: 15639
You must reload UITableView, and change height of either particular cell by reloading.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndexPath = indexPath;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath compare:selectedIndexPath] == NSOrderedSame) {
return 80;
}
return 40;
}
By this, the selected Row height will be more than others.
Thanks
Happy Coding!
Upvotes: 2