按钮状态激活错误的单元格 [英] Button state activates on wrong cells

查看:75
本文介绍了按钮状态激活错误的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将按钮添加到cell-s并添加了动作,所以如果用户触摸它,那么状态是不喜欢,如果用户再次触摸状态是Like。但是,状态也适用于其他单元格按钮。如果我快速滚动它只是随机选择什么单元格按钮应该有状态。是什么原因造成的?



我使用 cellForRowAt indexPath:IndexPath 函数调用按钮:



$ p $ cell.likeButton.addTarget(self,action:#selector(like),for:.touchUpInside)



这是分配给按钮的函数:

  func like(sender:UIButton){
let section = 0
let row = sender.tag
let indexPath = IndexPath(row:row,section:section)
let cell:FeedTableViewCell = tableView.dequeueReusableCell(withIdentifier:feedCell,for:indexPath)as! FeedTableViewCell
FIRDatabase.database()。reference()。child(posts)。child(postsArray [indexPath.row] .key).runTransactionBlock({(currentData:FIRMutableData) - > FIRTransactionResult in $ b $如果var post = currentData.value as?[String:AnyObject],let uid = FIRAuth.auth()?。currentUser?.uid {
var stars:Dictionary< String,Bool>
stars = post [stars] as?[String:Bool] ?? [:]
var starCount = post [starCount] as?Int ?? 0
if _ = stars [uid] {
//取消星标,删除星星
starCount - = 1
stars.removeValue(forKey:uid)
cell.likeButton.tag = indexPath.row
cell.likeButton.setTitle(Like,for:.normal)

cell.likeLabel.text =\(starCount)
} else {
//明星的职位,并添加自我明星
s tarCount + = 1
stars [uid] = true
cell.likeButton.tag = indexPath.row
cell.likeButton.setTitle(Dislike,for:.normal)

cell.likeLabel.text =\(starCount)
}
post [starCount] = starCount as AnyObject?
发布[星星] =星星为AnyObject?

//设置值并报告事务成功
currentData.value = post

返回FIRTransactionResult.success(withValue:currentData)
}
返回
中的FIRTransactionResult.success(withValue:currentData)
}){(错误,已提交,快照)if error = error {
print(error.localizedDescription)
}






$这样我创建了tableview的单元格: / p>

 覆盖func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {
let cell:FeedTableViewCell = tableView.dequeueReusableCell(withIdentifier:feedCell,for:indexPath)as! FeedTableViewCell

cell.likeButton.tag = indexPath.row
cell.likeButton.addTarget(self,action:#selector(self.tapped),for:.touchUpInside)
}

导致状态转移到其他按钮的原因是什么?我什至添加标签,所以它检测到选定的按钮。有没有什么与细胞重用?

它增加了喜欢Firebase的权利..

解决方案

我认为这个问题是因为你的细胞出了两次。你应该尝试;

  func like(sender:UIButton){
//你的代码...
让单元格:FeedTableViewCell = self.tableViewAddress.cellForRowAtIndexPath(indexPath)as! FeedTableViewCell
//您的代码...


I added button into cell-s and added action so if user touches it then the state is "Dislike" and if user touches again the state is "Like". However, the state applies to other cell buttons also. And if I scroll fast it just randomly picks what cell button should have the state. What causes this?

I call button with function inside cellForRowAt indexPath: IndexPath function like this:

cell.likeButton.addTarget(self, action: #selector(like), for: .touchUpInside)

And this is the function that is assigned to the button:

func like(sender: UIButton){
    let section = 0
    let row = sender.tag
    let indexPath = IndexPath(row: row, section: section)
    let cell: FeedTableViewCell = tableView.dequeueReusableCell(withIdentifier: "feedCell", for: indexPath) as! FeedTableViewCell
    FIRDatabase.database().reference().child("posts").child(postsArray[indexPath.row].key).runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
        if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
            var stars : Dictionary<String, Bool>
            stars = post["stars"] as? [String : Bool] ?? [:]
            var starCount = post["starCount"] as? Int ?? 0
            if let _ = stars[uid] {
                // Unstar the post and remove self from stars
                starCount -= 1
                stars.removeValue(forKey: uid)
                cell.likeButton.tag = indexPath.row
                cell.likeButton.setTitle("Like", for: .normal)

                cell.likeLabel.text = "\(starCount)"
            } else {
                // Star the post and add self to stars
                starCount += 1
                stars[uid] = true
                cell.likeButton.tag = indexPath.row
                cell.likeButton.setTitle("Dislike", for: .normal)

                cell.likeLabel.text = "\(starCount)"
            }
            post["starCount"] = starCount as AnyObject?
            post["stars"] = stars as AnyObject?

            // Set value and report transaction success
            currentData.value = post

            return FIRTransactionResult.success(withValue: currentData)
        }
        return FIRTransactionResult.success(withValue: currentData)
    }) { (error, committed, snapshot) in
        if let error = error {
            print(error.localizedDescription)
        }
    }
}

And like this I created the tableview with cells:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: FeedTableViewCell = tableView.dequeueReusableCell(withIdentifier: "feedCell", for: indexPath) as! FeedTableViewCell

        cell.likeButton.tag = indexPath.row
        cell.likeButton.addTarget(self, action: #selector(self.tapped), for: .touchUpInside)
    }

What causes the state to transfer to the other buttons also? I even added tags so it detects the selected button. Is there something to do with cell reuse?

It adds likes to Firebase to the right one..

解决方案

I think this issue is because of dequeuing your cell twice. you should try;

func like(sender: UIButton){
    //your code ...
    let cell: FeedTableViewCell = self.tableViewAddress.cellForRowAtIndexPath(indexPath) as! FeedTableViewCell
    //Your code ...

这篇关于按钮状态激活错误的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆