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

标签 ios uitableview swift autolayout custom-cell

我设计了一个 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/

相关文章:

iphone - libxml2.2.dylib 造成大量内存泄漏

uitableview - UITableView 背景上的 UIVisualEffectView

iphone - 如何在 swift ios 中将图像宽度与屏幕尺寸相匹配

swift - 如何在 Swift 中使用异步 Firebase 方法实例化一个类?

iphone - 第一次使用 swift(不是 objective-c)启动 iPhone 应用程序时,如何播放介绍视频?

ios - 如何在标签中写多行

ios - 如何更改App Store中ios应用程序的标题

ios - 查找顶部显示的 View UIActionSheet

ios - 如何为 tableview 的最后一个可见单元格创建不同的 View

ios - 无法检测到 ScrollView 中的 Swift UI 组件