IOS 8自动高度单元工作,但当我向上滚动它跳跃

时间:2015-03-12 09:50:05

标签: ios uitableview swift autolayout custom-cell

我设计了一个快速项目,基本上列出了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()

}

}

我无法找到任何解决方案。这需要我三天。 帮我解决跳转滚动问题。

谢谢,

1 个答案:

答案 0 :(得分:2)

我解决了这个问题!我做了两个步骤 首先,我自己计算我的自定义单元格的行高度

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

这就是全部。我希望它可以帮助你们。