[英]which cell button has pressed in tableView in swift 3
我已经读过类似的问题,但是这些代码对我没有用,所以请您帮忙:我有一个表格视图,每个单元格中都有一些按钮-该表格视图是用户的考虑因素,我在该表格视图中添加了按钮支付按钮时,该按钮隐藏在该单元格中的那个要素中;当用户未支付时,按钮在另一个单元格中又隐藏在另一个要素中,支付钱未被隐藏-这就是我想检测按钮按下了哪个单元格? 我知道在表格视图中我们可以检测到哪个单元格被按下,但这是不同的,因为用户只需按下该单元格中的“按钮”而不是按下所有单元格
由于我的代码太长,我只需要在此处查看表格视图中的代码
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "factorCell", for: indexPath) as! FactorCell
cell.factorTitle.text = factorTitle[indexPath.row]
cell.factorCode.text = factorCodes[indexPath.row]
cell.factorDate.text = factorDate[indexPath.row]
cell.factorHour.text = factorHour[indexPath.row]
cell.factorPrice.text = factorPrice[indexPath.row]
cell.factorCondition.text = factorConditions[indexPath.row]
cell.factorBill.tag = indexPath.row
cell.factorBill.addTarget(self,action:#selector(factorViewController.Bill), for: UIControlEvents.touchUpInside)
cell.factorDetail.tag = indexPath.row
cell.factorDetail.addTarget(self,action:#selector(factorViewController.FactorDetail), for: .touchUpInside)
if factorConditions[indexPath.row] == "پرداخت شده" {
cell.factorBill.isHidden = true
} else {
cell.factorCondition.textColor = UIColor.red
cell.factorBill.isHidden = false
}
return cell
}
这就是我想要的功能:当用户单击以支付其Factor并按下按钮时-移至另一个视图控制器,在该视图控制器中,用户可以在该视图控制器中看到这些数据-
**非常感谢,如果您不明白我想做什么告诉我,我将向您解释更多**
在自定义单元格类中为您的按钮创建IBAction,然后声明一个单击按钮时将调用的块。
var ButtonHandler:(()-> Void)!
@IBAction func ButtonClicked(_ sender: Any) {
self.ButtonHandler()
}
然后在tableview类的行单元格内,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = playlistTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
cell.ButtonHandler = {()-> Void in
// your code
}
return cell
}
您可以在cellForRowAt
中设置因子按钮的标记,以便可以轻松地从factorViewController.Bill获取factorViewController.Bill
,并且可以从该indexpath获取整个单元格,并且可以执行所需的任何操作。
你可以得到像细胞
let cell = tableView.cellForRowAtIndexPath(indexPath)
您可以参考此内容,以便了解如何从按钮单击获取索引路径!
当您按下factorBill
按钮时,您的Bill方法将被调用。因此您可以通过sender.tag
轻松区分Bill方法中的sender.tag
。您已经在cellForRowAt
设置了标签。
Here is update
just leave it, i explained it more simple,
create a button action in your cell class,then
Writre the code:-
> //Cell class
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
var delegateCell:CellDelegate?
var indexPath:IndexPath?
@IBAction func buttonCellClick(_ sender: Any) {
delegateCell?.didTapCell(index: indexPath!)
}
> //In ViewController
cell.delegateCell = self
cell.indexPath = indexPath
func didTapCell(index: IndexPath) {
print("PRESS BUTTON >> \(index.row)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.