我设计了一个 swift 项目,基本上在 tableview 中列出项目。我从服务器获取新数据并将它们添加到表的末尾。到目前为止,一切正常,但向上滚动时,它是跳转。我用这两个函数
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
return UITableViewAutomaticDimension
}
和
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
我的自定义单元类是基本的:
class StatusTableViewCell: UITableViewCell {
@IBOutlet weak var userNameLabel: UILabel!
@IBOutlet weak var statusBodyLabel: TTTAttributedLabel!
@IBOutlet weak var statusCreateTimeLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
statusBodyLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded()
}
我找不到任何解决方案。这需要我三天时间。 帮我解决跳跃滚动。
谢谢,
最佳答案
我来解决问题!我做了两个步骤。
首先,我像这个函数一样自己计算自定义单元格的行高
func heightForView(text:String, #font:UIFont, #width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
所以我的heightForRowAtIndexPath函数是这样的
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return heightForView(.....)
}
然后我删除了这个函数
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
总而言之,我自己计算行高,而不是使用 UITableViewAutomaticDimension 。然后我删除 heightForRowAtIndexPath 。
就是这样。我希望它能帮助你们。
关于IOS 8 自动高度单元工作但当我向上滚动时它跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006449/