在tableViewCell中,我有likeButton和differentButton。我想要的是当我按下likeButton时-将数据从单元格保存到Firebase中,而与众不同时-删除
但是我有两个问题:
1-例如当我单击cell == indexPath.row 0 or 1
中的likeButton时,在最后一个或上一个单元格上单击了likeButton
2--当我单击“我要删除的按钮”时,但这不起作用
在单元格中,我有:
var index: Int?
var cat = ""
var quoteNAme = ""
var quoteNum = ""
@IBAction func likePressed(_ sender: UIButton) {
sender.tag = index!
saveToUser(whereSave: "userLikes", quant: "quantLikes")
self.likeBtn.isEnabled = false
let ref = Database.database().reference().child("Цитаты").child("\(cat)").child("\(quoteNAme)").child("\(quoteNum)")
let keyToPost = ref.childByAutoId().key!
let updateLikes: [String : Any] = ["peopleWhoLike/\(keyToPost)" : Auth.auth().currentUser!.uid]
ref.updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in
if error == nil {
ref.observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as? [String : AnyObject] {
if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
let count = likes.count
self.likeLabel.text = "\(count)"
let update = ["quant" : count]
ref.updateChildValues(update)
self.likeBtn.isHidden = true
self.unlikeBtn.isHidden = false
self.likeBtn.isEnabled = true
}
}
})
}
})
ref.removeAllObservers()
}
}
@IBAction func unlikePressed(_ sender: UIButton) {
self.unlikeBtn.isEnabled = false
deleFromUser(whereSave: "userLikes", quant: "quantLikes")
let ref = Database.database().reference().child("Цитаты").child("\(cat)").child("\(quoteNAme)").child("\(quoteNum)")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let properties = snapshot.value as? [String : AnyObject] {
if let peopleWhoLike = properties["peopleWhoLike"] as? [String : AnyObject] {
for (id,person) in peopleWhoLike {
if person as? String == Auth.auth().currentUser!.uid {
ref.child("peopleWhoLike").child(id).removeValue(completionBlock: { (error, reff) in
if error == nil {
ref.observeSingleEvent(of: .value, with: { (snap) in
if let prop = snap.value as? [String : AnyObject] {
if let likes = prop["peopleWhoLike"] as? [String : AnyObject] {
let count = likes.count
self.likeLabel.text = "\(count)"
ref.updateChildValues(["quant" : count])
}else {
self.likeLabel.text = "0"
ref.updateChildValues(["quant" : 0])
}
}
})
}
})
self.likeBtn.isHidden = false
self.unlikeBtn.isHidden = true
self.unlikeBtn.isEnabled = true
break
}
}
}
}
})
ref.removeAllObservers()
}
保存和重新分组的功能
func saveToUser(whereSave:String, quant: String) {
let userUID = Auth.auth().currentUser!.uid
let ref = Database.database().reference().child("users").child("\(userUID)")
let keyToPost = ref.childByAutoId().key!
let mark = "Цитаты/\(cat)/\(quoteNAme)/\(quoteNum)"
let updateuser: [String : Any] = ["\(whereSave)/\(keyToPost)" : mark]
ref.updateChildValues(updateuser, withCompletionBlock: { (error, reff) in
if error == nil {
ref.observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as? [String : AnyObject] {
if let likes = properties["\(whereSave)"] as? [String : AnyObject] {
let count = likes.count
let update = ["\(quant)" : count]
ref.updateChildValues(update)
}
}
})
}
})
ref.removeAllObservers()
}
func deleFromUser(whereSave:String, quant: String){
let userUID = Auth.auth().currentUser!.uid
let mark = "Цитаты/\(cat)/\(quoteNAme)/\(quoteNum)"
let ref = Database.database().reference().child("users").child("\(userUID)")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let properties = snapshot.value as? [String : AnyObject] {
if let peopleWhoLike = properties["\(whereSave)"] as? [String : AnyObject] {
print("peopleWhoLike= \(peopleWhoLike)")
for (id,person) in peopleWhoLike {
if person as! String == mark {
ref.child("\(whereSave)").child(id).removeValue(completionBlock: { (error, reff) in
if error == nil {
ref.observeSingleEvent(of: .value, with: { (snap) in
if let prop = snap.value as? [String : AnyObject] {
print("prop=\(prop)")
if let likes = prop["\(whereSave)"] as? [String : AnyObject] {
let count = likes.count
ref.updateChildValues(["\(quant)" : count])
}else {
ref.updateChildValues(["\(quant)" : 0])
}
}
})
}
})
}
}
}
}
})
ref.removeAllObservers()
}
在ViewController中,我有:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryQuoteCell") as! CategoryQuoteCell
let tett = indexPath.row
let quant = quantLike[tett]
cell.quoteNAme = quoteNAme
cell.quoteNum = quotenumber[indexPath.row]
cell.cat = cat
cell.index = indexPath.row
cell.likeLabel.text = quant.description
cell.quoteTF.text = quoteList[tett]
cell.cellDelegate = self
for person in self.posts[indexPath.row].peopleWhoLike {
if person == Auth.auth().currentUser!.uid {
cell.likeBtn.isHidden = true
cell.unlikeBtn.isHidden = false
break
}
}
return cell
}
当像btn一样按时-工作正常并保存
最佳答案
我认为您执行的操作不正确,当您按时应将控件传递给Controller,而不是在tableViewCell中执行所有操作。而且,您还获得了先前的索引,因为在触摸单元格之后就获得了索引。所以我建议更改一些代码。在tableViewCell上
var onLikeTap: ((paramsThatYouWantToPass) -> Void)?
var onUnlikeTap: ((paramsThatYouWantToPass) -> Void)?
在您的表格视图单元格中,动作类似于:
@IBAction func likePressed(_ sender: UIButton) {
self.onLikeTap?(params)
}
同为异
@IBAction func unlikePressed(_ sender: UIButton) {
self.onUnlikeTap?(params)
}
然后在
cellForRowAt
方法的表视图上:func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryQuoteCell") as! CategoryQuoteCell
cell.onLikeButton = { [weak self] (params) in
// your like function that you used previously
}
cell.onUnlikeButton = { [weak self] (params) in
// your unlike function that you used previously
}
// other cell configuration that you used before
return cell
}
另外,如果您不希望将参数传递给like并且与闭包不同,则可以不带参数而保留它
关于ios - 像按钮一样快速火力,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52554957/