在tableViewCell中我有likeButton和unlikeButton。我想要的是当我按下 likeButton 时 - 将数据从单元格保存到 firebase ,当不同时 - 删除
但是我有两个问题:
1--当我在 cell == indexPath.row 0 或 1
中单击 likeButton 时,例如在最后一个或上一个单元格上单击 likeButton
2--当我单击likeButton时我想删除-但这不起作用
在单元格中我有:
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 -it' 时效果很好并保存
最佳答案
我认为你没有正确执行某些操作,当你按下时,你应该将控制权传递给 Controller ,而不是在 tableViewCell 中执行所有操作。而且您还将获得先前的索引,因为您将在触摸单元格后获得索引。所以我建议更改一些代码。在你的 tableViewCell 上
var onLikeTap: ((paramsThatYouWantToPass) -> Void)?
var onUnlikeTap: ((paramsThatYouWantToPass) -> Void)?
在表格 View 单元格中,类似于不同的操作:
@IBAction func likePressed(_ sender: UIButton) {
self.onLikeTap?(params)
}
相同与不同
@IBAction func unlikePressed(_ sender: UIButton) {
self.onUnlikeTap?(params)
}
然后在 TableView 上的 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
}
此外,如果您不想将参数传递给类似的东西,并且与闭包不同,您可以不使用参数
关于ios - 类似 swift firebase 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52554957/