IOS 8 个自动高度单元格有效,但当我向上滚动时它会跳转
IOS 8 auto height cell works but when i scroll up it jumps
我设计了一个 swift 项目,基本上在 table 视图中列出项目。我从服务器获取新数据并将它们添加到 table 的末尾。到目前为止,一切正常,但是向上滚动时,它是jump。我用这两个函数
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
return UITableViewAutomaticDimension
}
和
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
我的自定义单元格 class 是基本的:
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 。
就是这样。我希望它能帮助你们。
我设计了一个 swift 项目,基本上在 table 视图中列出项目。我从服务器获取新数据并将它们添加到 table 的末尾。到目前为止,一切正常,但是向上滚动时,它是jump。我用这两个函数
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
return UITableViewAutomaticDimension
}
和
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
我的自定义单元格 class 是基本的:
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 。
就是这样。我希望它能帮助你们。