【问题标题】:Custom height not being applied to UITableView Prototype Cell自定义高度未应用于 UITableView 原型单元
【发布时间】:2018-06-12 03:18:24
【问题描述】:

我正在构建一个表格视图场景,并且我已经为原型单元定义了一个自定义高度。我为自定义单元创建了一个文件,并通过该文件中的 IBOutlets 添加了一个图像和两个标签。我已经连接了表格视图并且表格正在调用信息,但是没有应用高度。这是单元格类和 cellForRow 方法的语法。

TableView 文章对象:

import Foundation
import UIKit

class FeedItem {

    var feedItemTitle: String
    var feedItemImage: UIImage
    var feedItemExcerpt: String = "Lorem ipsum dolor amet flexitarian art party skateboard, ethical pop-up drinking vinegar readymade humblebrag hot chicken. Retro kogi quinoa..."
    var feedItemLike: UIImage
    var feedItemComment: UIImage

    init(feedItemTitle: String, feedItemImage: UIImage, feedItemLike: UIImage, feedItemComment: UIImage) {
        self.feedItemTitle = feedItemTitle
        self.feedItemImage = feedItemImage
        self.feedItemLike = feedItemLike
        self.feedItemComment = feedItemComment
    }

}

表格视图单元格:

import UIKit

class FeedTableViewCell: UITableViewCell {

    @IBOutlet weak var articleImageView: UIImageView!
    @IBOutlet weak var articleTitleLabel: UILabel!
    @IBOutlet weak var articleExcerptLabel: UILabel!

}

TableView 扩展:

extension NewFeedViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return articlesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Article Cell", for: indexPath) as! FeedTableViewCell
        cell.articleImageView.image = articlesArray[indexPath.row].feedItemImage
        cell.articleTitleLabel.text = articlesArray[indexPath.row].feedItemTitle
        cell.articleExcerptLabel.text = articlesArray[indexPath.row].feedItemExcerpt
        return cell
    }

}

这是应用程序运行时表格视图的屏幕截图:

这是 Xcode 中 Prototype 单元格和 Size Inspector 的屏幕截图:

【问题讨论】:

  • 添加这个self.table.estimatedRowHeight = 44.0 ; self.table.rowHeight = UITableViewAutomaticDimension;

标签: ios swift xcode uitableview tableview


【解决方案1】:

viewDidLoad() 中使用 tableView.rowHeight = 400

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您必须在委托方法中明确指定行高。像这样:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 100.0;//Choose your custom row height
    }
    

    【讨论】:

    • 谢谢安迪,我很感激。
    【解决方案3】:

    1- 在 vi​​ewDidLoad 中使用动态 tableView

     tableView.estimatedRowHeight = 400;
    
     tableView.rowHeight = UITableViewAutomaticDimension;
    

    2- 不要实现 hightforCellAtIndexpath

    3-在单元格xib中从上到下正确调整约束

    4- 关于 collectionview , imageView 和 reply 部分最初给它们一个高度约束为零然后,将每个高度约束挂钩为 IBoulet 并根据 cellForRowAtIndexpath 更改它的常量值当前项通过更改约束的相应常量值来管理隐藏/显示

    5- 在返回单元格之前执行此操作

     cell.layoutSubviews()
    
     cell.layoutIfNeeded()
    
     return cell
    

    因此单元格可以重新布局以反映其子视图约束的变化

    【讨论】:

      【解决方案4】:

      使用此属性

      self.table.estimatedRowHeight = 44.0 
      self.table.rowHeight = UITableViewAutomaticDimension
      

      甚至你也可以使用委托

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableViewAutomaticDimension
      }
      
      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
          return 44
      }
      

      【讨论】:

        【解决方案5】:

        使用Delegate方法和heightForRowAt你们UITableView -

            func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
            {
                return 130.0;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-08-09
          • 2011-11-27
          • 1970-01-01
          • 2021-09-17
          • 2013-07-17
          • 1970-01-01
          • 1970-01-01
          • 2012-08-12
          • 2023-03-05
          相关资源
          最近更新 更多