背景:
我有一个带有2个投票按钮的自定义单元格的tableview,它可以从服务器中提取JSON数据,如果它说用户不允许投票,那么它会在禁用按钮的情况下加载单元格,而不是默认颜色蓝色。
问题:
当有人单击单元格上的按钮进行投票时,它会禁用/灰色单元格的按钮,但是当我滚动单元格并向后滚动时,它只会默认返回启用的蓝色按钮,直到我从表格中拉出来刷新它再次获取服务器数据。
我认为解决方案:
我应该修改存储用户是否可以投票的本地数组(disableArray)在点击的按钮的indexPath,但我不知道如何。
那么如何从我的自定义tableViewCell子类中的投票函数中访问和修改 disableArray
?
这是我的自定义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 :(得分:0)
是的,每次点击其中一个按钮时,您都应该更新disableArray
。你可以通过授权实现这一目标。
NSIndexPath
MainTableViewCell
内为indexPath和委托upVote
和downVote
方法传递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
方法中,配置Cell的cellDelegate
和indexPath
属性,如下所示:
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的ViewController和disableArray确认为MyCellProtocol
:
class ViewControllerOfYourTableView: UIViewController, MyCellProtocol {
在视图控制器中实现MyCellProtocol
。您可以在cellForRowAtIndexPath
方法之后立即执行此操作:
func updateDisableArray (indexPath: NSIndexPath) {
//**** update the array at the indexPath of the clicked button ****
self.disableArray[indexPath.row] = "1"
}