【发布时间】:2016-09-11 20:32:03
【问题描述】:
背景:
我有一个带有 2 个投票按钮的自定义单元格的表格视图,它从服务器中提取 JSON 数据,如果它说不允许用户投票,那么它会加载按钮禁用的单元格,并且颜色为灰色而不是默认的蓝色。
问题:
当有人单击单元格上的按钮进行投票时,它会禁用/灰色单元格的按钮,但是当我滚动经过单元格并向后滚动时,它只是默认回到启用的蓝色按钮,直到我从表格中拉出刷新它再次获取服务器数据。
我认为的解决方案:
我应该在被点击的按钮的 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
}
【问题讨论】:
标签: ios swift uitableview