我正在尝试以编程方式设计我的自定义 UITableView 单元格的布局。然而,我得到了一些奇怪的行为:尽管设置了 rowHeight,但前四行是默认高度,其余的是我指定的。这会影响我的单元格的设计,因为我部分地根据行高以编程方式布置标签。
我初始化 TableView 如下:
func gameTableInit() {
gameTableView = UITableView()
gameTableView.delegate = self
gameTableView.dataSource = self
let navFrame = self.navigationController?.view.frame
gameTableView.frame = CGRect(x: navFrame!.minX, y: navFrame!.maxY, width: self.view.frame.width, height: self.view.frame.height - navFrame!.height - (self.tabBarController?.view.frame.height)!)
gameTableView.rowHeight = gameTableView.frame.height/4 //HEIGHT OF TABLEVIEW CELL
gameTableView.register(GameCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(gameTableView)
}
这是我的 cellForRowAtIndexPath 函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! GameCell
print("height of cell: \(cell.frame.height)")
var game = Game()
game.awayTeam = "CLE"
game.homeTeam = "GSW"
cell.setUIFromGame(g: game)
return cell
}
打印语句打印:
height of cell: 44.0
height of cell: 44.0
height of cell: 44.0
height of cell: 44.0
经过一些滚动后,它会打印预期的内容:
height of cell: 166.75
...
我创建了一个名为 GameCell 的自定义 tableview 单元格
class GameCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
positionUIElements()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//on left
var awayTeamAbbrev = UILabel()
var awayTeamLogo: UIImage!
//on right
var homeTeamAbbrev = UILabel()
var homeTeamLogo: UIImage!
func positionUIElements() {
//away team abbreviation is centered on upper left half of cell
awayTeamAbbrev.frame = CGRect(x: self.frame.minX, y: self.frame.minY, width: self.frame.midX, height: self.frame.height/4)
awayTeamAbbrev.textAlignment = .center
awayTeamAbbrev.adjustsFontSizeToFitWidth = true
//awayTeamLogo
//home team abbreviation is centered on upper right half of cell
homeTeamAbbrev.frame = CGRect(x: self.frame.midX, y: self.frame.minY, width: self.frame.midX, height: self.frame.height/4)
homeTeamAbbrev.textAlignment = .center
homeTeamAbbrev.adjustsFontSizeToFitWidth = true
//homeTeamLogo
self.contentView.addSubview(awayTeamAbbrev)
self.contentView.addSubview(homeTeamAbbrev)
}
func setUIFromGame(g: Game) {
awayTeamAbbrev.text = g.awayTeam!
homeTeamAbbrev.text = g.homeTeam!
}
}
我已经尝试过许多在线建议的答案,例如调用 layoutIfNeeded,但在我尝试过的任何地方都不起作用。
最佳答案
虽然接受的答案在某些情况下会有所帮助,但我想分享一些其他方法来解决此问题:
- 检查单元组件的Content Hugging Priority(尤其是当您使用单元格内的堆栈 View 时)
在你的单元格中添加:
override func didMoveToSuperview() { super.didMoveToSuperview() layoutIfNeeded()
关于ios - Swift 3 不正确的初始 UITableViewCell 行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42577003/