revi
revi

Reputation: 45

TableViewCell auto height

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

Answers (3)

daris mathew
daris mathew

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.

enter image description here

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

Bhargav B. Bajani
Bhargav B. Bajani

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

Nimitt Nemade
Nimitt Nemade

Reputation: 1

To get dynamically updated height

-add these methods

===========

Objective C

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

======

Swift

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Upvotes: 0

Related Questions