Pushpendra
Pushpendra

Reputation: 1519

didselectrowatindexpath called after long pressed in swift

I am trying to select content on select Cell From the Table, Here i am using didselectrowatindexpath method but it getting call after long pressed the cell.

It might be duplicate question but i try lot's of solution but my problem did not get fix Here code which is i am using

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return autocompleteUrls.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    let cell  = tableView.dequeueReusableCellWithIdentifier("AutoCompleteRowIdentifier", forIndexPath: indexPath) as! DrawerTableViewCell
    let index = indexPath.row as Int
    
    cell.autoCompleteLabel!.text = autocompleteUrls[index].email!
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.row)!")
    
    print("didSelectRowAtIndexPath")
    let selectedCell   = autocompleteTableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell
    print("Selected Table Text =\(selectedCell.autoCompleteLabel!.text)")
    textEmail.text = selectedCell.autoCompleteLabel!.text
    autocompleteTableView.hidden = true
    
}

And my viewDidLoad is as

   override func viewDidLoad() {
    pastUrls = defaults.objectForKey("autoCompleteEmail") as? [String] ?? [String]()
    spinnerInitialization()
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    autocompleteTableView.tableFooterView = UIView()
    autocompleteTableView.hidden = true
    autocompleteTableView.delegate = self
    autocompleteTableView.dataSource = self
    textEmail.delegate = self
}

Updated:- Code Of hideKeyboardWhenTappedAround

    extension UIViewController {
func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
    view.endEditing(true)
}}

Upvotes: 0

Views: 785

Answers (3)

Make the UserInteraction enable for the label present inside the cell.

And Verify that the cell has UserInteraction id enable.

Upvotes: 0

Pushpendra
Pushpendra

Reputation: 1519

Thanks Dev.RK Here I was using code for to hide the key board where i was using view.addGestureRecognizer(tap) method in hideKeyboardWhenTappedAround() @Dev.Rk Suggest me to add a line before view.addGestureRecognizer(tap) The line is tap.cancelsTouchesInView=false and that resolve my crazy problem

Upvotes: 4

Akshansh Thakur
Akshansh Thakur

Reputation: 5331

You need to explain your structure. I am assuming that you are using a tableView inside the UIViewController. But, why do you have the following line inside your didSelect method?

 let selectedCell   = autocompleteTableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell

instead of

let selectedCell   = tableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell

I understand that you have name of table view specified as autocompleteTableView, but isnt that table view's reference coming to you from the didSelectRow method itself?

Upvotes: 0

Related Questions