![](/img/trans.png)
[英]UITableView second prototype cell does not appear to be respecting custom height?
[英]Custom height not being applied to UITableView Prototype Cell
我正在构建一个表视图场景,我已经为原型单元定义了一个自定义高度。 我为自定义单元格创建了一个文件,并通过该文件中的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
}
}
TableView单元格:
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
甚至你也可以使用代表
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
您必须在委托方法中明确指定行高。 像这样:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0;//Choose your custom row height
}
在viewDidLoad()
使用tableView.rowHeight = 400
希望能帮助到你!
1-在viewDidLoad中使用动态tableView
tableView.estimatedRowHeight = 400;
tableView.rowHeight = UITableViewAutomaticDimension;
2-不要实现hightforCellAtIndexpath
3-从上到下在单元xib中正确调整约束
4-关于集合视图,imageView和reply部分首先给它们所有的高度约束为零,然后将每个高度约束挂钩为IBOulet并根据当前项目在cellForRowAtIndexpath中更改它的常量值通过更改隐藏/显示来管理隐藏/显示相应的约束常数值
在返回单元格之前执行此操作
cell.layoutSubviews()
cell.layoutIfNeeded()
return cell
因此可以对单元格进行转发以反映其子视图约束的变化
使用Delegate
方法和你的UITableView
heightForRowAt
-
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 130.0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.