当单元格滚动时,Tableview设置会消失

时间:2016-09-11 20:32:03

标签: ios swift uitableview

背景
我有一个带有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
        }

1 个答案:

答案 0 :(得分:0)

是的,每次点击其中一个按钮时,您都应该更新disableArray。你可以通过授权实现这一目标。

  1. 创建一个采用NSIndexPath
  2. 的协议
  3. MainTableViewCell内为indexPath和委托
  4. 创建两个变量
  5. 调用委托并从upVotedownVote方法传递indexPath。
  6. 现在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)      
        }
    }
    
    1. 在表格视图的cellForRowAtIndexPath方法中,配置Cell的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]
      
    2. 使包含tableView的ViewController和disableArray确认为MyCellProtocol

      class ViewControllerOfYourTableView: UIViewController, MyCellProtocol {
      
    3. 在视图控制器中实现MyCellProtocol。您可以在cellForRowAtIndexPath方法之后立即执行此操作:

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