背景:
我有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
}