gpt4 book ai didi

ios - TableView 中的不同长度单元格在滚动 Swift 时使 View 对齐

转载 作者:行者123 更新时间:2023-11-28 08:51:37 25 4
gpt4 key购买 nike

所以我有一个带有 2 个原型(prototype)单元格的 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

}

}

所以第二个原型(prototype)单元格只有两个标签,其值是从字典中分配的。单元格大小需要根据文本行数进行扩展或收缩。在自动布局中,我将行数设置为 0,因此它会选择需要多少行。这工作正常,除非当您在应用程序中滚动时,它会在用户从底部向上滚动时捕捉 View 。有没有办法避免这种情况?

在此先感谢您的帮助。

最佳答案

我花了更多时间寻找后发现了这个:

http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

它给了我我需要的东西。我删除了代码中设置 rowHeight 的部分,然后使用 viewDidLoad 方法和自动布局来限制我的单元格大小,经过一些反复试验后,它可以正常工作,而不会在您滚动时捕捉到位置。

关于ios - TableView 中的不同长度单元格在滚动 Swift 时使 View 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com