TableView 中的不同长度单元格在滚动时使视图对齐 Swift
Varying length cells in TableView make the view snap while scrolling Swift
所以我有一个带有 2 个原型单元格的 TableView。这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell
cell.centerImage.image = mainImage
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
tableView.rowHeight = cell.labelBlack.frame.height + 40
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
let bodyString = details[headingString]
let labelWidth = Int(cell.body.frame.width)
println(labelWidth)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000))
label.text = bodyString
label.numberOfLines = 100
label.font = UIFont(name: "OpenSans-Light", size: 12.0)
label.sizeToFit()
tableView.rowHeight = label.frame.height + 3
return cell
}
}
所以第二个原型单元格只有两个标签,其值是从字典中分配的。单元格大小需要根据文本行数进行扩展或收缩。在自动布局中,我将行数设置为 0,因此它会选择需要多少行。这工作正常,除非当您在应用程序中滚动时,它会在用户从底部向上滚动时捕捉视图。有没有办法避免这种情况?
在此先感谢您的帮助。
我花了更多时间寻找后发现了这个:
它给了我我需要的东西。我删除了代码中设置 rowHeight 的部分,然后使用 viewDidLoad 方法和自动布局来限制我的单元格大小,经过一些反复试验后,它可以正常工作,而不会在滚动时捕捉到位置。
您正在更改表格视图的行高。这很可能是非常错误的。 tableView 的 rowHeight 是所有没有自己高度的行的默认值,因此您实际上是在更改 all 单元格的高度。
你应该使用委托方法tableView:heightForRowAtIndexPath:
所以我有一个带有 2 个原型单元格的 TableView。这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell
cell.centerImage.image = mainImage
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
tableView.rowHeight = cell.labelBlack.frame.height + 40
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
let bodyString = details[headingString]
let labelWidth = Int(cell.body.frame.width)
println(labelWidth)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000))
label.text = bodyString
label.numberOfLines = 100
label.font = UIFont(name: "OpenSans-Light", size: 12.0)
label.sizeToFit()
tableView.rowHeight = label.frame.height + 3
return cell
}
}
所以第二个原型单元格只有两个标签,其值是从字典中分配的。单元格大小需要根据文本行数进行扩展或收缩。在自动布局中,我将行数设置为 0,因此它会选择需要多少行。这工作正常,除非当您在应用程序中滚动时,它会在用户从底部向上滚动时捕捉视图。有没有办法避免这种情况?
在此先感谢您的帮助。
我花了更多时间寻找后发现了这个:
它给了我我需要的东西。我删除了代码中设置 rowHeight 的部分,然后使用 viewDidLoad 方法和自动布局来限制我的单元格大小,经过一些反复试验后,它可以正常工作,而不会在滚动时捕捉到位置。
您正在更改表格视图的行高。这很可能是非常错误的。 tableView 的 rowHeight 是所有没有自己高度的行的默认值,因此您实际上是在更改 all 单元格的高度。
你应该使用委托方法tableView:heightForRowAtIndexPath: