gpt4 book ai didi

swift - 仅将 IBAction 应用于单个单元格

转载 作者:搜寻专家 更新时间:2023-10-31 23:02:17 25 4
gpt4 key购买 nike

我有一个带有原型(prototype)单元格的 tableView;有了这个func我设置单元格高度

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return cellHeight
}

有了这个action我更改了 UIButton 内部的单元格高度

@IBAction func changeCellHeight(sender: UIButton)
{
if cellHeight == 44
{
cellHeight = 88
} else {
cellHeight = 44
}
tableView.beginUpdates()
tableView.endUpdates()
}

现在我只需要为选定的单元格(而不是每个单元格)更改高度;所以我定义 var index: NSIndexPath!我实现了这个 func

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
index = self.tableView.indexPathForSelectedRow()!
println("Cella " + "\(index)")
}

正如我在控制台中预期的那样,Xcode 打印了选定的单元格 ( <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})。

所以我的麻烦是如何使用var indexIBAction .

提前致谢。

最佳答案

要根据选择更改高度,如果您已经实现了 didSelectRowAtIndexPath 方法,则不需要 IBAction。

只需先在您的 didSelectRowAtIndexPath 方法中稍作更改-

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
// You don't need to do this -> index=self.tableView.indexPathForSelectedRow()!
index = indexPath;
println("Cella " + "\(index)")

tableView.beginUpdates()
tableView.endUpdates()
}

然后对您的 heightForRowAtIndexPath 方法稍作更改-

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if index == indexPath {
return 88
}
else{
return 44
}
}

关于swift - 仅将 IBAction 应用于单个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247194/

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