到目前为止,在我的项目中,我已成功实现了一个tableview,其单元格填充了FIR数据库中的帖子。
我无法确定要添加的代码,以便在按下消息用户按钮时,识别并提取特定于该单元的海报的用户ID。如果我能够做到这一点,我可以提取其他用户的其他信息,以便我可以设置一个聊天,其中包括消息的userID,userFirstName和profile pic的接收者。
我猜测标记按钮是第一步,但我不确定如何获得特定于该帖子的用户ID。
我甚至没有显示代码,因为我对如何做到这一点毫无头绪....
答案 0 :(得分:3)
将按钮放在单元格中。还可以创建自定义单元格类,并将userId或user对象放在该单元格中。当您点击该按钮时,您可以从该单元格中获取userId等。 或者如果你想从cell到viewcontroller获取该事件,你可以传递delegate来查看viewcontroller。像这样的事情
//cellforrowatindexpath
cell.delegate = self
cell.indexPath = indexPath
// Your cell class
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
@IBAction func buttonAction(_ sender: AnyObject) {
self.delegate?.didTapCell(index: indexPath)
}
答案 1 :(得分:1)
//tag the cell button
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("HealerProfileCell", forIndexPath: indexPath) as! yourTVCell
cell.detailsButton.tag = indexPath.row
cell.detailsButton.addTarget(self, action: #selector(HealersTableViewController.performHealerDetailsInfoSegue(_:)), forControlEvents: .TouchUpInside)
return cell
}
//make a function
func performHealerDetailsInfoSegue(sender: AnyObject?) {
performSegueWithIdentifier("your segue identifier", sender: sender)
}
//use prepare for segue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let object = yourArray[(sender!.tag)]
if segue.identifier == "your segue identifier" {
let dvc = segue.destinationViewController as! YourVC
dvc.object = object
}
}
答案 2 :(得分:0)
在TableView cellForItemAtIndexPath
方法中为您的按钮添加目标。
cell.yourButton.addTarget(self, action: #selector(CellButtonClicked), for: .touchUpInside)
然后添加按下按钮时调用的此函数...
func CellButtonClicked(sender: UIButton) {
print("Clicked")
guard let cellInAction = sender.superview as? YourCell else { return }
guard let din = yourTableView?.indexPath(for: cellInAction) else { return }
// retrieve the specific values in the cell from your array
let specificCellData = yourArray[(indexPath as NSIndexPath).row]
}