2016-03-05 172 views
0

我在UITableViewCell中有一个标签,我希望我的身高TableViewCell是根据标签高度自动调整的。TableViewCell自动高度

- (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 }

+0

您使用自动布局??? –

+0

是的.. @ El Capitan – revi

+0

不只是在heightForRowAtIndexPath中返回'UITableViewAutomaticDimension' ..但条件是你的约束应该正确附加 –

回答

2

用于动态地高度的UITableViewCell在viewDidLoad中设置每个内容高度

_tableView.estimatedRowHeight = 100.0; 
_tableView.rowHeight = UITableViewAutomaticDimension; 

这一套自动细胞高度

让我知道,如果这个工程...

https://stackoverflow.com/a/35055320/5085393

或方法自动尺寸

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

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 100; 
} 
+0

不工作myFriend。@ bhargav bajani – revi

+0

replace return 165;与UITableViewAutomaticDimension in heightForRowAtIndexPath方法 –

+0

不工作 - (CGFloat)tableView:(UITableView *)tableView UITableViewAutomaticDimension:(NSIndexPath *)indexPath {0} {0} {0} {0}返回UITableViewAutomaticDimension; } – revi

0

设置标签动态

删除

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

**设置表行高度**

_tableView.estimatedRowHeight = 150.0; 
_tableView.rowHeight = UITableViewAutomaticDimension; 

而且还CHEAK标签高度的约束没有设置

0

为了得到动态更新的高度

-add这些方法

===========

Objective C

- (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 
}