简体   繁体   English

tableview单元格内的UIButton更改文本

[英]UIButton inside tableview cell change text

Well i know these types of questions are answered by SO before, but mine is little different so please read it. 好吧,我知道这类问题以前都是由SO回答的,但是我的问题几乎没有什么不同,所以请仔细阅读。

I am using a button inside tableview cell. 我在tableview单元格内使用按钮。 On click of button i am showing an AlertViewController with list of actions. 在单击按钮时,我正在显示带有操作列表的AlertViewController。 On selecting action my button text and my custom model class propery is changed. 在选择动作时,我的按钮文本和自定义模型类属性已更改。

Button text is changing on button click. 单击按钮时,按钮文本正在更改。 But my problem is cell not storing button state. 但是我的问题是单元不存储按钮状态。 If i change button of 1st cell and then click on second cell button all other buttons back to its default text. 如果我更改第一个单元格的按钮,然后单击第二个单元格按钮,则所有其他按钮返回其默认文本。

please see this video 请看这部影片

Here is my code 这是我的代码

setting cell data 设置单元格数据

    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 on button click 单击按钮时的动作

//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)
    }

Please hemp me. 请麻我。

In cellForRowAtIndexPath method Set button text like this way First remove this cellForRowAtIndexPath方法中以这种方式设置按钮文本首先将其删除

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

Replace this 取代这个

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

And one more thing you need to increase cell.dbColumnOrder width. 还需要增加cell.dbColumnOrder宽度。

Hope it works 希望能奏效

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

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