ios - 在单元格中实现 Like 按钮 - Swift 逻辑/数据传递问题

标签 ios swift uitableview model-view-controller voting-system

当我点击按钮时,我希望仅更新该单元格的值。

我是个小白,所以请对我宽容点!

我目前在 tableViewController 中访问的自定义单元格中设置了一个委托(delegate),以及确定是否按下按钮以及是否应启用按钮的 Bool 值。该按钮默认设置为 true。我在 Parse 中创建了一个名为 isEnabled 的列,为按钮提供了默认值 true

目前,当我点击按钮时,每隔 3 个单元格就会发生变化(这意味着它不会仅针对该 1 个单元格发送操作)。但是,当打印到日志时,日志显示该值已从 true 更改为 false,只有在我向上滚动之后...

为什么值没有立即改变?

这是我的自定义单元代码:

protocol CellButtonDelegate {

    func buttonClicked(cell : PFTableViewCell)

}

var delegate : CellButtonDelegate?

class FeedTableViewCell: PFTableViewCell {
     var delegate : CellButtonDelegate?

    internal var buttonEnabled : Bool? {

        get {

            return happyOutlet.enabled

        } set {

            happyOutlet.enabled = newValue!

        }

    }
   @IBOutlet weak var happyOutlet: UIButton!

 @IBAction func happyBtn(sender: AnyObject) {


        if(parseObject != nil) {

            if var votes: Int? = parseObject!.objectForKey("votes") as? Int {

                votes!++
                parseObject!.setObject(votes!, forKey: "votes")
                parseObject!.saveInBackground()

                votesLabel?.text = "\(votes!)"
               // print(votes)
            }

        }

        delegate?.buttonClicked(self)
        viewBlackYellow.backgroundColor = UIColor.yellowColor()

        happyOutlet.enabled = false
        sadOutlet.enabled = false
        happyOutlet.alpha = 0
        sadOutlet.alpha = 0
        //These values all get updated for every 3rd cell

    }

这是我的 cellForRowAtIndexPath 代码(投票的值以及我拥有的其他值都被缓存):

numberOfRowsInSection 基于滚动显示,因为当我设置单元格数量以返回依赖于滚动的对象时,值/单元格会被缓存:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        objectArray = objects //the objects array is cached (using PFQuery function to query the values, so the cells are cached as well)
        return objects!.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

            var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell

     objects![indexPath.row]


            if let pfObject = objectArray?[indexPath.row] {

                cell?.parseObject = object
                var votes: Int? = pfObject["votes"] as? Int
                if votes == nil {
                    votes = 0
                }

                cell?.delegate = self


                let isEnabled = pfObject["isEnabled"] as! Int



 if isEnabled == 1 {

                cell?.buttonEnabled = true

            } else  {

                cell?.buttonEnabled = false
                print("its false")

            }
       return cell
     } 

 func buttonClicked(cell: PFTableViewCell) {

        let indexPath = tableView.indexPathForCell(cell)

        var object : PFObject = objectArray![indexPath!.row] as! PFObject
        object["isEnabled"] = 0

        objectArray?.removeAtIndex((indexPath?.row)!)
        objectArray?.insert(object, atIndex: (indexPath?.row)!)
    }

如何才能使单元格值和操作不重复?我假设所有逻辑不应该在自定义单元格中,而应该在 tableVC 中。然而,我尝试实现这一点,但没有任何运气(我从 happyOutlet.tag 发送了一个操作)。

最后,我有 self.tableView.reloadData()/self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)

两者都会使屏幕抖动,因为我还在单元格中缓存了视频。有时(奇怪的是)它也没有帮助。如果我可以避免重新加载 tableView 那将是最好的。

我们非常感谢任何和所有建议、问题、评论和改进。

我引用了此链接以及其他链接,但它们没有帮助:Implementing a like button in a tableviewCell in swift这没有帮助。

最佳答案

将逻辑移动到 View Controller 会更像 MVC。将您的按钮设置为公共(public) IBOutlet,以便在 cellForRowAtIndexPath 中您可以说: happyOutlet.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

然后说:

func buttonClicked(sender:UIButton) {
    let location = sender.convertPoint(sender.bounds.origin, toView: self.tableView)

    let indexPath = self.tableView.indexPathForRowAtPoint(location)

    var object : PFObject = objectArray![indexPath!.row] as! PFObject
    object["isEnabled"] = 0

    objectArray?.removeAtIndex((indexPath?.row)!)
    objectArray?.insert(object, atIndex: (indexPath?.row)!)

    if let cell:CustomTableViewCell = self.tableView.cellForRowAtIndexPath(indexPath) as? CustomTableViewCell {
    //update your UI
    }
}

您还应该记住,当您使单元出队时,其 UI 将与其最后状态匹配。所以如果你说这样的话:

        cell.happyOutlet.enabled = false
        cell.happyOutlet.alpha = 0
        cell.viewBlackYellow.backgroundColor = UIColor.yellowColor()

在一个 Action 中。然后,在将单元格出列后,在 cellForRowAtIndexPath 中,您会想说如下内容:

        cell.happyOutlet.enabled = true
        cell.happyOutlet.alpha = 1.0
        cell.viewBlackYellow.backgroundColor = UIColor.whiteColor()//Or whatever your default background color is

将单元格重置为其默认值。我猜想屏幕上可以同时显示大约三个单元格,所以想一下为什么三分之一会受到按钮按下的影响。

关于ios - 在单元格中实现 Like 按钮 - Swift 逻辑/数据传递问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001951/

相关文章:

ios - 为什么我在左侧边栏的 xcode 中看不到我工作区中的所有文件?

ios - 如何在核心数据中保存明天日期任务以及如何将这些任务放入 Swift 的 TableView 中?

ios - iOS 13 默认显示系统表情符号键盘

ios - UIScrollview 的元素没有响应

ios - 录制期间的 AVCaptureConnection setVideoOrientation

ios - MPMoviePlayerController 黑色窗口,不加载音频文件

ios - 无法在名称处存储类型为 _SwiftValue 的对象。只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。

xcode - 如何在 Swift 中从具有多种内容类型的字典制作表格?

objective-c - 在弹出 View 中从 tableViewController 访问父 View Controller

ios - UutableView 选定的单元格到其他 View