gpt4 book ai didi

ios - tableview 单元格中的 UIButton 更改文本

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:34 24 4
gpt4 key购买 nike

我知道 SO 之前已经回答过这些类型的问题,但我的问题略有不同,所以请阅读。

我在 tableview 单元格中使用了一个按钮。单击按钮时,我将显示一个带有操作列表的 AlertViewController。在选择操作时,我的按钮文本和我的自定义模型类属性发生了变化。

按钮文本在按钮点击时发生变化。但我的问题是单元格不存储按钮状态。如果我更改第一个单元格的按钮,然后单击第二个单元格按钮,所有其他按钮将恢复为默认文本。

please see this video

这是我的代码

设置单元格数据

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

let column = columns[indexPath.row]
cell.dbColumnName.text = column.name
cell.dbColumnOrder.titleLabel?.text = column.order
cell.dbColumnOrder.tag = indexPath.row

print(columns)
cell.dbColumnOrder.addTarget(self, action: #selector(ViewController.showAlert(_:)), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}

按钮点击 Action

//MARK:Show Alert
func showAlert(sender:UIButton){
let alert = UIAlertController(title: "Column Order", message: "Please select column order", preferredStyle: .ActionSheet)
let index = sender.tag
let indexPath = NSIndexPath(forRow: index, inSection: 0)

alert.addAction(UIAlertAction(title: "None", style: .Default, handler: { (action) in
//execute some code when this option is selected
self.columns[index].order = "None"
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}))

alert.addAction(UIAlertAction(title: "Assending", style: .Default, handler: { (action) in
//execute some code when this option is selected
self.columns[index].order = "Assending"
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}))

alert.addAction(UIAlertAction(title: "Desending", style: .Default, handler: { (action) in
//execute some code when this option is selected
self.columns[index].order = "Desending"
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}))

self.presentViewController(alert, animated: true, completion: nil)
}

请帮帮我。

最佳答案

cellForRowAtIndexPath 方法中这样设置按钮文本先去掉这个

cell.dbColumnOrder.titleLabel?.text = column.order

替换这个

cell.dbColumnOrder.setTitle("\(column.order)", for: .normal)

您还需要增加 cell.dbColumnOrder 宽度。

希望它有用

关于ios - tableview 单元格中的 UIButton 更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41012800/

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