gpt4 book ai didi

ios - 如何在 Swift 中为不同 TableView 单元格中的按钮提供不同的标签

转载 作者:可可西里 更新时间:2023-11-01 01:29:17 25 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中编写不同主题的条目,然后可以对条目给出上下分。我在我的 tableViewController 中使用了这个函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

我在此函数的末尾添加了这两行:

cell.plusButton.tag = indexPath.row
cell.minusButton.tag = indexPath.row

所以这应该给 tableView 中的每个按钮一个标签,以便它与该单元格的 indexpath.row 相同,我错了吗?因为当我尝试调用按钮时,它们的所有标签都相同且等于 0。我如何给它们不同的标签?这样就没有办法了吗?

这是我想调用按钮时的代码:

@IBAction func plus(sender: AnyObject) {

print(self.tag)

let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+entriesArrayTwo[self.tag])

var value = Int()
var date = String()
var user = String()
var votedDown = [""]
var votedUp = [""]

ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
let dict = snapshot.value as! [String: AnyObject]

value = dict["point"] as! Int
date = String(dict["date"]!)
user = String(dict["user"]!)
votedUp = dict["votedUp"] as! NSArray as! [String]
votedDown = dict["votedDown"] as! NSArray as! [String]

var tempBool = false
var temp = -1

for uid in votedDown {
temp = temp + 1

if uid == FIRAuth.auth()?.currentUser?.uid {
votedDown.removeAtIndex(temp)
tempBool = true
}
}

if tempBool == false {
votedUp.append((FIRAuth.auth()?.currentUser?.uid)!)
}

ref.setValue(["point": value+1, "date": date, "user": user, "votedDown": votedDown, "votedUp": votedUp])

self.point.text = String(value+1)
})

if minusButton.hidden == true {
minusButton.hidden = false
} else {
plusButton.hidden = true
}
}

我的 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 函数如下:

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

if self.resultSearchController.isActive {

let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+filteredTableData[(indexPath as NSIndexPath).row])

ref.observeSingleEvent(of: .value, with: { snapshot in

let value = snapshot.value as? NSDictionary

cell.point.text = String(describing: value!["point"]!)

let postRef = FIRDatabase.database().reference().child("users/"+String(describing: value!["user"]!))

postRef.observeSingleEvent(of: .value, with: { snapshotTwo in

let valueTwo = snapshotTwo.value as? NSDictionary

cell.subInfo.text = String(describing: valueTwo!["name"]!)+" "+String(describing: valueTwo!["surname"]!)+" - "+String(describing: value!["date"]!)

})


})

cell.entry.text = self.filteredTableData[(indexPath as NSIndexPath).row]

} else {

let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+entriesArray[(indexPath as NSIndexPath).row])

ref.observeSingleEvent(of: .value, with: { snapshot in

let value = snapshot.value as? NSDictionary

cell.point.text = String(describing: value!["point"]!)

let postRef = FIRDatabase.database().reference().child("users/"+String(describing: value!["user"]!))

postRef.observeSingleEvent(of: .value, with: { snapshotTwo in

let valueTwo = snapshotTwo.value as? NSDictionary

cell.subInfo.text = String(describing: valueTwo!["name"]!)+" "+String(describing: valueTwo!["surname"]!)+" - "+String(describing: value!["date"]!)

})

let votedUpRef = ref.child("votedUp")

votedUpRef.observeSingleEvent(of: .value, with: { upSnapshot in

var tempDict = snapshot.value as! [String: AnyObject]
let tempArray = tempDict["votedUp"] as! [String]

for uid in tempArray {

if String(uid) == FIRAuth.auth()?.currentUser?.uid {

cell.plusButton.isHidden = true

}

}

})

let votedDownRef = ref.child("votedDown")

votedUpRef.observeSingleEvent(of: .value, with: { upSnapshot in

var tempDict = snapshot.value as! [String: AnyObject]
let tempArray = tempDict["votedDown"] as! [String]

for uid in tempArray {

if String(uid) == FIRAuth.auth()?.currentUser?.uid {

cell.minusButton.isHidden = true

}

}

})

})

cell.entry.text = self.entriesArray[(indexPath as NSIndexPath).row]

}

cell.plusButton.tag = (indexPath as NSIndexPath).row
cell.minusButton.tag = (indexPath as NSIndexPath).row

// NEW METHOD TO GET THE BUTTON

let check1: UIButton = (cell.viewWithTag(1) as! UIButton)
let check2: UIButton = (cell.viewWithTag(2) as! UIButton)
check1.addTarget(self, action: #selector(HubEntriesTableViewController.CloseMethod(_:event:)), for: .touchDown)
check2.addTarget(self, action: #selector(HubEntriesTableViewController.CloseMethod1(_:event:)), for: .touchDown)

// Configure the cell...

return cell
}

最佳答案

也许终于找到问题了。当我在我的项目中重现该问题时,我意识到缺少downcasting to UIButton

所以在 HubEntryTableViewCell 子类中更新方法如下:

@IBAction func plus(sender: AnyObject) {

// self.tag, if called from UITableViewCell subclass, is rather cell's tag, not button's tag

let button = sender as! UIButton
print("button.tag = \(button.tag)")
...
}

关于ios - 如何在 Swift 中为不同 TableView 单元格中的按钮提供不同的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com