gpt4 book ai didi

iOS/swift : issues with UITableViewCell height

转载 作者:搜寻专家 更新时间:2023-11-01 07:24:43 24 4
gpt4 key购买 nike

我写了一个UITableView,里面有自定义的UITableViewCell,数据加载和表格数据填充没有问题,但是Cell高度错了,内容每个单元格只有一半可见(请参阅随附的屏幕截图)

tableview cell xib

我有一个 .xib 文件(如上面第二个屏幕截图所示)用于 UITableViewCell 并编写了一个类来覆盖 UITableViewCell 使用下面的 layoutSubviews() 实现类,我在其中使用了 [Neon][3] 库来进行 UI 元素的定位/布局:

override func layoutSubviews() {
super.layoutSubviews()
self.contentView.clipsToBounds = true


avatarImage.anchorInCorner(.TopLeft, xPad: 2, yPad: 2, width: 15, height: 15)
postedByUsername.align(.ToTheRightCentered, relativeTo: avatarImage, padding: 1, width: 190, height: 15)

timeAgo.anchorInCorner(.TopRight, xPad: 2, yPad: 2, width: 130, height: 15)

questionTitle.alignAndFill(align: .UnderMatchingLeft, relativeTo: avatarImage, padding: 3)
firstAttachedImage.align(.ToTheRightMatchingTop, relativeTo: questionTitle, padding: 3, width: 40, height: 40)

self.answerButton.setFAText(prefixText: "", icon: FAType.FAReply, postfixText: "2", size: 14, forState: .Normal)
self.commentButton.setFAText(prefixText: "", icon: FAType.FACommentO, postfixText: "1", size: 14, forState: .Normal)
self.likeButton.setFAText(prefixText: "", icon: FAType.FAHeartO, postfixText: "10", size: 14, forState: .Normal)

answerCommentLikeStackView.align(.UnderMatchingLeft, relativeTo: questionTitle, padding: 3, width: 190, height: 30)

self.setNeedsUpdateConstraints()
}

在我的 UITableViewController 实现中,我设置单元格/行高如下:

 self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 150
self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine

// disable horizontal scroll
self.tableView.contentSize = CGSizeMake(self.tableView.frame.size.width, self.tableView.contentSize.height)

但是cell的高度还是不对,还有什么方法可以让高度看起来正确呢?

最佳答案

不要设置 tableView.rowHeight = x,而是尝试覆盖 heightForRowAtIndexPath 方法:

override func tableView(tableView: UITableView, heightForRowAtIndexPathindexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

应该为 TableView 呈现的每个单元格调用它并相应地调整它的大小。

关于iOS/swift : issues with UITableViewCell height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com