2016-12-07 65 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男人 –