Reputation: 1338
I have read similar questions like this but these codes didn't worked for me so please help : I have a table view with some buttons in each cells - This table view is factor for user and I added button to that tableview - when user has paid money the button is hidden in that factor in that cell and in another cell for another factor when user didn't paid the pay money is not hidden - this is what I did I want to detect which cells button has pressed ?! I know that in table view we can detect which cell has pressed But this is different because the user just press the Button in that cell Not press all of the Cell
As my codes are too long I Just Put the table view Codes here
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
}
and this is the function that I want to: when User Click to pay his Factor and press the button - Move to another view Controller and in that view controller the user could see these data here in that View Controller --
**Thanks a lot If you didn't understand What I want to do tell me and i will Explain More **
Upvotes: 1
Views: 4443
Reputation: 933
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)")
}
Upvotes: 1
Reputation: 615
When you press factorBill
button then your Bill method will call.So you can easily differentiate the button inside your Bill method by sender.tag
.You already set the tag in cellForRowAt
.
Upvotes: 1
Reputation: 27448
You are setting factor button's tag in your cellForRowAt
so you can easily get indexpath from your factorViewController.Bill
, and from that indexpath you can get your whole cell and you can do whatever you need.
You can get cell like,
let cell = tableView.cellForRowAtIndexPath(indexPath)
you can refer this so post to know how to get indexpath from button click!
Upvotes: 2
Reputation: 5694
Create IBAction for you button in the custom cell class then declare a block that will be called on button click.
var ButtonHandler:(()-> Void)!
@IBAction func ButtonClicked(_ sender: Any) {
self.ButtonHandler()
}
Then in your tableview class inside the cell for row method,
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
}
Upvotes: 8