0

i don't know what happen, i set the button.tag with the table row and when it reach row > 1, it will throw lldb. it works if the button.tag <= 1

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cells")! as UITableViewCell
    let alertBtn = cell.viewWithTag(1) as! UIButton;
    alertBtn.tag = indexPath.row
    alertBtn.addTarget(self, action: Selector(("showAlert:")), for: UIControlEvents.touchUpInside)
    return cell
}

enter image description here

Abizern
  • 146,289
  • 39
  • 203
  • 257
Benny Wijaya
  • 207
  • 3
  • 16
  • Where does it throws error.can you show us implemetation of showAlert. – luckyShubhra Jul 28 '17 at 05:21
  • the error is from when i set the button tag, even though i remove the add target, it will keep throw the same error. it doesn't related with the add target – Benny Wijaya Jul 28 '17 at 05:22
  • What are you trying to do in let alertBtn = cell.viewWithTag(1) as! UIButton; line. Here you are saying that the view with tag 1 is your button and then you again want to change the tag so in the reuse state your tag would vary and probable cause of crash would be this line. Why do you want to get the button from view with tag. – Kapil G Jul 28 '17 at 05:23
  • Where you have added ` alertBtn` ? – Prashant Tukadiya Jul 28 '17 at 05:24
  • Can you show your custom cell swift file – Kapil G Jul 28 '17 at 05:24
  • ok i feel or button tag gets conflict with viewWithTag. Are u using custom cell or uitableviewcell. – luckyShubhra Jul 28 '17 at 05:26
  • i am using uitableviewcell @luckyShubhra – Benny Wijaya Jul 28 '17 at 05:26
  • so the button tag can just be set once ? @kapsym – Benny Wijaya Jul 28 '17 at 05:27
  • the button is from storyboard tableview cell @mikeAlter – Benny Wijaya Jul 28 '17 at 05:28
  • It should be unique and ideally related to the position of cell most of the time to identify it uniquely. @BennyWijaya – Kapil G Jul 28 '17 at 05:28
  • I don't think button is a part of standard UITableViewCell so you have to use custom cells. Check this - https://stackoverflow.com/questions/24170922/creating-custom-tableview-cells-in-swift/36426858#36426858 – Kapil G Jul 28 '17 at 05:31
  • You would need to create a custom cell class and then to identify the button you can directly do cell.alertBtn and then set the tag accordingly. – Kapil G Jul 28 '17 at 05:32
  • take IBOutlet for button From UITableViewCell instead of refreshing with tag. – Ujesh Jul 28 '17 at 05:32
  • @BennyWijaya After seeing the link i shared, let me know if you need any more help in understanding and i will help out on the code side – Kapil G Jul 28 '17 at 05:49
  • If you want to get indexpath of tapped button you can get it without tag. Check my ans below – luckyShubhra Jul 28 '17 at 05:49

4 Answers4

1

Application crash on this line, because it fails to find a view with tag 1, the tag is updating in every cell with row value.

let alertBtn = cell.viewWithTag(1) as! UIButton

remove this line and Take @IBOutlet for alertBtn From UITableViewCell instead of refreshing with tag.

Ujesh
  • 1,698
  • 2
  • 23
  • 35
0

If you want to get indexPath of cell containing tapped button you can use function similar to this matching your requirement.

 func showAlert(sender: AnyObject) {
         if let cell = sender.superview?.superview as? UITableViewCell{ // do check your viewchierarchy in your case
              let indexPath = itemTable.indexPath(for: cell)
           }
         print(indexPath)// you can use this indexpath to get index of tapped button
     }

Remove this line from cellForRowAtIndexPath alertBtn.tag = indexPath.row

If you can use Custom Cell for this purpose you can get indexpath of selected button as you were getting previously.

Create CustomCell and create IBOutlet for your button and labels etc. You can access subviews of your cell in cellForRowAtIndexPath and assign tag to your button. If you have any queries regarding CustomCell do let me know.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
0

Swift 3X...

You are replacing your tag so first tag items are getting nil so replace this code ...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cells")! as UITableViewCell
let alertBtn = cell.viewWithTag(1) as! UIButton
alertBtn.addTarget(self, action: #selcetor(showAlert(sender:))), for: .touchUpInside)
return cell
 }

func showAlert(sender:UIButton) {
        let point = sender.convert(CGPoint.zero, to: self.tableview)
        let indexpath =  self.tableview.indexPathForRow(at: point)
}
Kallz
  • 3,244
  • 1
  • 20
  • 38
Raksha Saini
  • 604
  • 12
  • 28
  • 1
    thanks it works!!!, but the add target nowadays should be like this . alertBtn.addTarget(self, action: #selector(showAlert(sender:)), for: .touchUpInside) – Benny Wijaya Jul 28 '17 at 06:07
0

Try to do custom UITableViewCell.

Declare protocol and delegate for Your new class class. Wire up a action and call delegate

protocol MyCellDelegate: class {
    func buttonPressed(for cell: MyCell)
}

class MyCell:UITableViewCell {

    weak var delegate: MyCellDelegate?

    @IBAction func buttonPressed(sender: Any){
        self.delegate?.buttonPressed(for: self)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    .......
    cell.delegate = self

    ........
}

Remember to add new protocol implementation to Your VC. You can add prepareForReuse method and reset delegate to nil when cell is reused.

Prettygeek
  • 2,461
  • 3
  • 22
  • 44