我有一个带自定义单元格的UITableViewController。我“不想”识别所选单元格,但在TableView中识别所选标签本身。与Instagram类似,当我选择用户时,它应该转到用户页面,而如果我选择注释,它会推送到同一单元格内发生的另一个注释VC。
我从这个link中学到了添加手势识别器的功能,但是它正在崩溃我。
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
我的代码如下
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!
}
}
}
}
}
答案 0 :(得分:0)
将您的代码更新为:
let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel:")
self.tableView.addGestureRecognizer(userLabelRecognizer)
答案 1 :(得分:0)
如果您使用didSelectUserLabel
let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel")
你需要实现像
func didSelectUserLabel()
{
}
如果您使用didSelectUserLabel:
let userLabelRecognizer = UITapGestureRecognizer(target: self, action: "didSelectUserLabel:")
你需要实现像
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!
}
}
}
}
}