[英]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.