2016-09-11 3 views
3

Hintergrund:
Ich habe eine Tableview mit einer benutzerdefinierten Zelle mit 2 Stimme Tasten, die JSON-Daten von dem Server zieht, wenn es der Benutzer sagt, nicht zu wählen ist erlaubt , dann lädt es die Zelle mit deaktivierten Schaltflächen und grau anstatt des Standardblues.Einstellungen eines Tabellen- weggeht, wenn die Zelle weg gescrollt wird

Problem:
Wenn jemand einen Knopf auf der Zelle klickt zu stimmen, es deaktiviert/Grauton mit den Tasten für die Zelle, aber wenn ich scrollen Vergangenheit der Zelle und zurückblättern, es ist nur Standardwerte zurück zu aktivieren, damit blaue Tasten bis ich zum Aktualisieren von der Tabelle ziehe und es die Serverdaten wieder holt.

Was ich denke, ist die Lösung:
Ich soll das lokale Array wird geändert wird, speichert, ob der Benutzer abstimmen oder nicht (disableArray) am indexPath der Schaltfläche, die geklickt wurde, aber ich weiß nicht, Wie.

So wie kann ich zugreifen und die tableViewCell UnterklassedisableArrayder Abstimmung Funktionen in meiner benutzerdefinierten ändern?

hier ist der Code für meine benutzerdefinierte 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)  
     } 
    } 

und der Code für meine 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 
     } 

Antwort

0

Ja Sie Ihre disableArray jedes Mal auf eine der Schaltflächen geklickt aktualisieren sollten. Das können Sie mit Delegation erreichen.

  1. ein Protokoll erstellen, die eine NSIndexPath
  2. Erstellen zwei Variablen innerhalb MainTableViewCell indexPath für die Delegierten und delegieren
  3. Anruf entgegennimmt und die indexPath von upVote und downVote Methoden übergeben.

Jetzt ist der Code für MainTableViewCell sollte wie folgt aussehen:

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)  
    } 
} 
  1. Im cellForRowAtIndexPath Methode Ihrer Tabellenansicht konfigurieren Sie die cellDelegate und indexPath Eigenschaften der Zelle , wie folgt aus:

    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] 
    
  2. die Viewcontroller machen, dass die Tableview und Ihre disa enthält bleArray zu MyCellProtocol bestätigen:

    class ViewControllerOfYourTableView: UIViewController, MyCellProtocol { 
    
  3. MyCellProtocol im View-Controller implementieren. können Sie diese direkt nach dem cellForRowAtIndexPath Methode setzen:

    func updateDisableArray (indexPath: NSIndexPath) { 
        //**** update the array at the indexPath of the clicked button **** 
        self.disableArray[indexPath.row] = "1" 
    } 
    
Verwandte Themen