0

I have a UITableViewController with custom cell. I "do not want" to recognise the selected cell, but recognise the selected label itself inside a TableView. Similar to Instagram, when I select user, it should go to the user page, while if I select comment, it pushes to another comment VC that happens all within the same cell.

I learnt from this link to add gesture recogniser, however it is crashing on me.

016-02-10 17:53:20.490 SocialMe[1395:476081] -[SocialMe.LatestPostViewController didSelectUserLabel]: unrecognized selector sent to instance 0x13deac830
2016-02-10 17:53:20.495 SocialMe[1395:476081] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocialMe.LatestPostViewController didSelectUserLabel]: unrecognized selector sent to instance 0x13deac830'
*** First throw call stack:
(0x18267d900 0x181cebf80 0x18268461c 0x1826815b8 0x18258568c 0x1878f0dbc 0x1875145b8 0x1873a29b0 0x1878f23bc 0x187361b58 0x18735e8dc 0x1873a0820 0x18739fe1c 0x1873704cc 0x18736e794 0x182634efc 0x182634990 0x182632690 0x182561680 0x183a70088 0x1873d8d90 0x100077794 0x1821028b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

My codes as follow

class LatestPostViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Latest Post"
        self.refreshControl?.beginRefreshing()
        GetLatestPost()

        let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel")
        self.tableView.addGestureRecognizer(userLabelRecognizer)


        self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    }

    func didSelectUserLabel(recognizer: UITapGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Ended {
            let swipeLocation = recognizer.locationInView(self.tableView)
            if let swipedIndexPath = tableView.indexPathForRowAtPoint(swipeLocation) {
                if let swipedCell = self.tableView.cellForRowAtIndexPath(swipedIndexPath) {
                    // Swipe happened. Do stuff!
                }
            }
        }
    }
}
Community
  • 1
  • 1
jo3birdtalk
  • 616
  • 5
  • 20

2 Answers2

0

Update your code to :

    let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel:")
    self.tableView.addGestureRecognizer(userLabelRecognizer)
Aruna Mudnoor
  • 4,795
  • 14
  • 16
0

if you using like didSelectUserLabel

let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel")

you need to implement like

func didSelectUserLabel()
{

}

if you using like didSelectUserLabel:

let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel:")

you need to implement like

func didSelectUserLabel(recognizer: UITapGestureRecognizer) {
    if recognizer.state == UIGestureRecognizerState.Ended {
        let swipeLocation = recognizer.locationInView(self.tableView)
        if let swipedIndexPath = tableView.indexPathForRowAtPoint(swipeLocation) {
            if let swipedCell = self.tableView.cellForRowAtIndexPath(swipedIndexPath) {
                // Swipe happened. Do stuff!
            }
        }
    }
}
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143