2016-09-11 46 views
3

背景
我有2個投票按鈕,從服務器拉JSON數據,如果它說用戶是不允許投票定製細胞的tableview ,然後它加載禁用按鈕的單元格,並使用灰色代替默認的藍色。泰伯維設置要離開時,細胞滾動遠

問題
當有人點擊該單元的按鈕進行投票,它禁用/灰色按鈕的小區,但是當我滾過細胞和向後滾動,它只是默認回到啓用藍色按鈕直到我從表中刷新並重新獲取服務器數據。

我認爲是解決
我應該修改本地陣列存儲用戶是否可以被點擊按鈕的indexPath投票或不(disableArray),但我不知道怎麼樣。

所以,我怎麼能訪問和修改自投功能disableArray我的自定義tableViewCell子類裏面?

這裏是我的自定義tableViewCell代碼:

import UIKit 

    class MainTableViewCell: UITableViewCell { 

     @IBOutlet weak var totalvoteLabel: UILabel! 
     @IBOutlet weak var upButton: UIButton! 
     @IBOutlet weak var downButton: UIButton! 

     var number: Int?{ 
      return Int(totalvoteLabel.text!) 
     } 

     override func awakeFromNib() { 
      super.awakeFromNib() 
     } 


     @IBAction func downvote(sender: AnyObject) { 
      totalvoteLabel.text = String(number! - 1) 
      upButton.userInteractionEnabled = false 
      downButton.userInteractionEnabled = false 
      upButton.setImage(UIImage(named: "upArrowGray.png"), forState: UIControlState.Normal) 
      downButton.setImage(UIImage(named: "downArrowGray.png"), forState: UIControlState.Normal) 
      totalvoteLabel.textColor = UIColor(red: 188/255, green: 183/255, blue: 174/255, alpha: 1.0) 

     } 
     @IBAction func upvote(sender: AnyObject) { 
      totalvoteLabel.text = String(number! + 1) 
      upButton.userInteractionEnabled = false 
      downButton.userInteractionEnabled = false 
      upButton.setImage(UIImage(named: "upArrowGray.png"), forState: UIControlState.Normal) 
      downButton.setImage(UIImage(named: "downArrowGray.png"), forState: UIControlState.Normal) 
      totalvoteLabel.textColor = UIColor(red: 188/255, green: 183/255, blue: 174/255, alpha: 1.0)  
     } 
    } 

和我的tableView代碼:

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1 
     } 
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
          return postArray.count 
      } 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

      let cell : MainTableViewCell! = tableView.dequeueReusableCellWithIdentifier("MainTableViewCell") as! MainTableViewCell 

      cell.totalvoteLabel.text = voteArray[indexPath.row] 
      cell.ID = idArray[indexPath.row] 

      if(disableArray[indexPath.row] == "1"){ 
       cell.upButton.userInteractionEnabled = false 
       cell.downButton.userInteractionEnabled = false 
       cell.upButton.setImage(UIImage(named: "upArrowGray.png"), forState: UIControlState.Normal) 
       cell.downButton.setImage(UIImage(named: "downArrowGray.png"), forState: UIControlState.Normal) 
       cell.totalvoteLabel.textColor = UIColor(red: 188/255, green: 183/255, blue: 174/255, alpha: 1.0) 
       cell.disable = true 
      }else{ 
       cell.upButton.userInteractionEnabled = true 
       cell.downButton.userInteractionEnabled = true 
       cell.upButton.setImage(UIImage(named: "upArrow.png"), forState: UIControlState.Normal) 
       cell.downButton.setImage(UIImage(named: "downArrow.png"), forState: UIControlState.Normal) 
       cell.totalvoteLabel.textColor = UIColor(red: 43/255, green: 192/255, blue: 228/255, alpha: 1.0) 
      } 

      return cell as MainTableViewCell 
     } 

回答

0

是的,你應該將disableArray被點擊的按鈕中的一個,每次更新。你可以通過委派實現這一目標。

  1. 創建一個協議,其採用NSIndexPath
  2. 爲indexPath創建內部MainTableViewCell兩個變量和委派
  3. 呼叫委託和從upVotedownVote方法通過indexPath。

現在對於MainTableViewCell代碼應該是這樣的:

import UIKit 

//**** The Protocol **** 
protocol MyCellProtocol { 
    func updateDisableArray (indexPath: NSIndexPath) 
} 

class MainTableViewCell: UITableViewCell { 

    @IBOutlet weak var totalvoteLabel: UILabel! 
    @IBOutlet weak var upButton: UIButton! 
    @IBOutlet weak var downButton: UIButton! 

    var number: Int?{ 
     return Int(totalvoteLabel.text!) 
    } 

    //**** Variable to store the indexPath **** 
    var indexPath: NSIndexPath! 

    //**** Variable for the delegate **** 
    var cellDelegate: MyCellProtocol? 

    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 


    @IBAction func downvote(sender: AnyObject) { 
     totalvoteLabel.text = String(number! - 1) 
     upButton.userInteractionEnabled = false 
     downButton.userInteractionEnabled = false 
     upButton.setImage(UIImage(named: "upArrowGray.png"), forState: UIControlState.Normal) 
     downButton.setImage(UIImage(named: "downArrowGray.png"), forState: UIControlState.Normal) 
     totalvoteLabel.textColor = UIColor(red: 188/255, green: 183/255, blue: 174/255, alpha: 1.0) 

     //**** Call the delegate method with the indexPath **** 
     self.cellDelegate?.updateDisableArray(indexPath) 

    } 
    @IBAction func upvote(sender: AnyObject) { 
     totalvoteLabel.text = String(number! + 1) 
     upButton.userInteractionEnabled = false 
     downButton.userInteractionEnabled = false 
     upButton.setImage(UIImage(named: "upArrowGray.png"), forState: UIControlState.Normal) 
     downButton.setImage(UIImage(named: "downArrowGray.png"), forState: UIControlState.Normal) 
     totalvoteLabel.textColor = UIColor(red: 188/255, green: 183/255, blue: 174/255, alpha: 1.0) 

     //**** Call the delegate method with the indexPath **** 
     self.cellDelegate?.updateDisableArray(indexPath)  
    } 
} 
  • 在表格視圖的cellForRowAtIndexPath方法配置的小區的cellDelegateindexPath屬性,像這樣:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    
        let cell : MainTableViewCell! = tableView.dequeueReusableCellWithIdentifier("MainTableViewCell") as! MainTableViewCell 
    
        //**** Set self to be the cellDelegate **** 
        cell.cellDelegate = self 
    
        //**** Set the indexPath property of the cell **** 
        cell.indexPath = indexPath 
    
        cell.totalvoteLabel.text = voteArray[indexPath.row] 
    
  • 使包含的tableView和你DISA的視圖控制器bleArray確認到MyCellProtocol

    class ViewControllerOfYourTableView: UIViewController, MyCellProtocol { 
    
  • 在視圖控制器實現MyCellProtocol

    func updateDisableArray (indexPath: NSIndexPath) { 
        //**** update the array at the indexPath of the clicked button **** 
        self.disableArray[indexPath.row] = "1" 
    } 
    
  • :您可以將 cellForRowAtIndexPath方法後,就把這個權利