繁体   English   中英

tableview单元格内的UIButton更改文本

[英]UIButton inside tableview cell change text

好吧,我知道这类问题以前都是由SO回答的,但是我的问题几乎没有什么不同,所以请仔细阅读。

我在tableview单元格内使用按钮。 在单击按钮时,我正在显示带有操作列表的AlertViewController。 在选择动作时,我的按钮文本和自定义模型类属性已更改。

单击按钮时,按钮文本正在更改。 但是我的问题是单元不存储按钮状态。 如果我更改第一个单元格的按钮,然后单击第二个单元格按钮,则所有其他按钮返回其默认文本。

请看这部影片

这是我的代码

设置单元格数据

    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
    }

单击按钮时的动作

//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宽度。

希望能奏效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM