ios - 当单元格滚动离开时,Tableview 设置消失

标签 ios swift uitableview

背景:
我有一个带有自定义单元格的表格 View ,该单元格带有 2 个投票按钮,可从服务器中提取 JSON 数据,如果它表示不允许用户投票,则它会加载按钮禁用且颜色为灰色而不是默认蓝色的单元格。

问题:
当有人单击单元格上的按钮进行投票时,它会禁用/变灰该单元格的按钮,但是当我滚动经过该单元格并向后滚动时,它只是默认返回启用的蓝色按钮,直到我从表格中拉出并刷新它再次获取服务器数据。

我认为的解决方案:
我应该在被单击的按钮的 indexPath 处修改存储用户是否可以投票的本地数组 (disableArray),但我不知道该怎么做。

那么我如何从自定义 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
        }

最佳答案

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

  1. 创建一个采用NSIndexPath
  2. 的协议(protocol)
  3. MainTableViewCell 中为 indexPath 和委托(delegate)创建两个变量
  4. 调用委托(delegate)并从 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. 在 TableView 的 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. 在 View Controller 中实现MyCellProtocol。你可以把它放在 cellForRowAtIndexPath 方法之后:

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

关于ios - 当单元格滚动离开时,Tableview 设置消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39440372/

相关文章:

swift - 为 UISearchBar 设置 cornerRadius?

swift - 使用 cocoapods 时无法在新的 swift 文件上导入框架

ios - 使用用户的私钥签署任何消息并在以太坊上验证签名

iphone - 使用 UITableViewCell 使 UIView 出队

objective-c - 让按钮覆盖其他进程

iOS url请求问题

ios - 尽管有标签和按钮,可重用的 UITableViewCells 显示为 null

iphone - 设置 UITableViewCell 的动态高度,其中仅包含图像(可变高度)

ios - UITapGestureRecognizer 和 addTarget 之间的区别

ios - float 在 UIView 上的水平 ScrollView 而不是 Paging