gpt4 book ai didi

ios - 更改单个单元格 UITableView 的高度

转载 作者:IT王子 更新时间:2023-10-29 05:16:45 26 4
gpt4 key购买 nike

我想要如下内容:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

if indexPath == 3{

cell.height = "50dp"

}

return cell

}

解决此问题的替代方法或最简单的方法是什么?

编辑

我是否可以同时指定节号:即-

if sectionIndex == 5

最佳答案

我建议使用 heightForRowAtIndexPath 函数。 TableView 将调用此函数来确定特定索引路径的行高。

示例代码:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == SECTION_INDEX {
return 60
} else {
return UITableViewAutomaticDimension
}
}

swift 4:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == SECTION_INDEX {
return 60
} else {
return UITableViewAutomaticDimension
}
}

稍微解释一下:indexPath 有两个属性:section 和 row。通过检查这两个属性,您可以制作自己的控制流来确定每个单元格的高度。

如果您返回 60,则表示您希望单元格的高度为 60 磅。如果您返回 UITableViewAutomaticDimension,您希望系统为您决定最佳高度(在这种情况下,您最好为 Storyboard中的单元格设置自动布局)。

我还建议你在 iTunes U 上学习 stanford course CS193p,这对你会有很大帮助:)

关于ios - 更改单个单元格 UITableView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31155795/

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