2016-12-07 70 views
1

嗯,我知道这些类型的问题是由SO之前回答的,但我的有点不同,所以请阅读它。UIButton tableview cell change text

我在tableview单元格内使用了一个按钮。点击按钮我显示一个AlertViewController的动作列表。在选择动作我的按钮文本和我的自定义模型类propery被改变。

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

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 
    } 

按钮操作点击

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

请麻我。

+0

你可能会显示什么是截图或GIF的确切问题?我很难明白可能是什么问题。 – dirtydanee

+0

@dirtydanee我添加了一个链接,请访问它 –

回答

2

cellForRowAtIndexPath方法设置按钮上的文字像这样 首先删除此

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

替换此

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

而且一个你需要增加cell.dbColumnOrder宽度件事。

希望它的作品

+0

thnx很多....你救了我的生命....它的工作就像一个魅力....你能解释我什么是你的代码和我之间的防御。 ..technically –

+0

更新按钮的标题时,您始终需要指定ControlState! –

+0

好的......... thnx男人 –