【问题标题】:TableViewCell auto heightUITableViewCell 自动高度
【发布时间】:2016-06-19 04:22:32
【问题描述】:

我在 UITableViewCell 中有一个标签,我希望我的高度 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 }

【问题讨论】:

  • 你在使用自动布局???
  • 不仅仅是在 heightForRowAtIndexPath 中返回 UITableViewAutomaticDimension .. 但条件是您的约束应该正确附加

标签: objective-c iphone autolayout uitableview


【解决方案1】:

首先转到storyboard 上的UITableViewCell 标签的属性检查器,并将换行 属性设置为Word Wrap。现在标签的约束很重要。

只给Top、Bottom、Leading、Trailing约束,这样单元格就可以根据标签的内容调整其高度。

然后在您的viewDidLoad 或为表格视图设置delegate 的位置添加以下代码,

self.yourTableView.estimatedRowHeight = 52.0; // Your desired minimum height for the cell.
self.yourTableView.rowHeight = UITableViewAutomaticDimension;

【讨论】:

    【解决方案2】:

    在 viewDidLoad 中设置 UITableViewCell 的动态高度

    _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;
    }
    

    【讨论】:

    • 替换返回165;在 heightForRowAtIndexPath 方法中使用 UITableViewAutomaticDimension
    • 不工作- (CGFloat)tableView:(UITableView *)tableView UITableViewAutomaticDimension:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
    【解决方案3】:

    动态更新高度

    -添加这些方法

    ============

    目标 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-03
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2013-10-21
      • 2011-06-20
      相关资源
      最近更新 更多