gpt4 book ai didi

ios - Swift iOS 自动调整 UITableViewCell() 的 UILabel 高度

转载 作者:行者123 更新时间:2023-11-30 13:28:28 27 4
gpt4 key购买 nike

如何让它根据textLabel的长度自动调整每个单元格的高度?理想情况下,我希望 textLabel 也能被换行,因此 textLabel 可以占用它需要的任意多行。以下代码是我目前拥有的代码:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let notification = self.fetchedTweetsArray[indexPath.row]

let cell = UITableViewCell()
cell.textLabel?.text = notification
return cell
}

最佳答案

首先你的 cellForRowAtIndex 应该是这样的

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("ID_CustomCell", forIndexPath: indexPath) as! CustomCell

cell.selectionStyle = UITableViewCellSelectionStyle.None

cell.lblData.text = "CustomCell....."
return cell

}

UILabel 的行数应为零。

enter image description here

现在,根据 UILabel 的高度,您必须计算每个单元格的动态高度并将其保存在可变数组中,以便稍后您可以在 heightForRowAtIndex 中使用。

确保 UILabel 上像这样自动调整大小

enter image description here

刚刚做了一个示例代码,大家可以下载观察。

Download sample code

关于ios - Swift iOS 自动调整 UITableViewCell() 的 UILabel 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855523/

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