【问题标题】:Tableview settings going away when the cell is scrolled away滚动单元格时表格视图设置消失
【发布时间】: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


    【解决方案1】:

    是的,您应该在每次单击其中一个按钮时更新您的disableArray。您可以通过委托来实现。

    1. 创建一个采用NSIndexPath 的协议
    2. MainTableViewCell 中为 indexPath 和委托创建两个变量
    3. 调用代理并从upVotedownVote 方法传递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)      
        }
    }
    
    1. 在表格视图的cellForRowAtIndexPath 方法中配置单元格的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 和 disableArray 的 ViewController 确认到 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"
      }
      

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多