Reputation: 45
I have a label in the UITableViewCell, and I want my height TableViewCell is auto according label height.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TWTTweetTableViewCell *cell = (TWTTweetTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TWTTweetTableViewCell" forIndexPath:indexPath];TWTTweet *tweet = self.tweets[indexPath.row]; cell.tweetMessage.text = tweet.tweetMessage; cell.timestamp.text = [tweet howLongAgo]; cell.tag = indexPath.row; TWTUser *user = [[TWTTwitterAPI sharedInstance] userForId:tweet.userId]; cell.user.text = user.username; return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 165; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Tapped a tweet }
Upvotes: 1
Views: 4759
Reputation: 429
First go to the UITableViewCell
label's attribute inspector on the storyboard
and set the Line Break property to Word Wrap. Now the constraint's for the label are important.
Give only Top, Bottom, Leading, Trailing constraint, this way the cell can adjust its height based on the content of the label.
Then either in your viewDidLoad
or the place where you set delegate
for the tableview add the below code,
self.yourTableView.estimatedRowHeight = 52.0; // Your desired minimum height for the cell.
self.yourTableView.rowHeight = UITableViewAutomaticDimension;
Upvotes: 0
Reputation: 927
for dynamically height for UITableViewCell set in viewDidLoad
_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;
this set automatic cell height as per content height
Let me know if this works...
https://stackoverflow.com/a/35055320/5085393
or as method for Automatic Dimension
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
Upvotes: 5
Reputation: 1
To get dynamically updated height
-add these methods
===========
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
======
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Upvotes: 0