0

I have a UITableView inside of another view. Whenever I click on a cell in the table, I would like that cell to be highlighted until it is clicked again to deselect it. Using the didSelectRowAtIndexPath method, I have accomplished this. However, the cell selection takes a long time. I have to hold down on a cell for 3 seconds before it highlights the cell, rather than it being instantaneous. How do I get it to select the cell the instant it is touched?

Here is my relevant code.

class AddDataViewController : UIViewController {

    @IBOutlet weak var locationTableView: UITableView!  

    var fstViewController : FirstViewController?

    let locationTableViewController = LocationTableViewController()

    override func viewWillAppear(animated: Bool) {
        // Set the data source and delgate for the tables
        self.locationTableView.delegate = self.locationTableViewController
        self.locationTableView.dataSource = self.locationTableViewController

        // Set the cell separator style of the tables to none
        self.locationTableView.separatorStyle = UITableViewCellSeparatorStyle.None

        // Refresh the table
        self.locationTableView.reloadData()
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        // Create a tap gesture recognizer for dismissing the keyboard
        let tapRecognizer = UITapGestureRecognizer()

        // Set the action of the tap gesture recognizer
        tapRecognizer.addTarget(self, action: "dismissKeyboard")

        // Add the tap gesture recognizer to the view
        //self.view.addGestureRecognizer(tapRecognizer)
    }
}

class LocationTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return tableView.frame.height / 5
    }

    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
    }

    override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.blueColor()
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")

        cell.selectionStyle = UITableViewCellSelectionStyle.None

        cell.textLabel!.text = "Test"

        return cell
    }
}
Jason247
  • 974
  • 4
  • 16
  • 38

3 Answers3

2

The problem was the UITapGestureRecognizer was interfering with the tap of the cell. I apologize that the tap gesture code was not in my initial post as I did not realize that could be the culprit. I have added it into the code snippet in the original post.

Jason247
  • 974
  • 4
  • 16
  • 38
  • Ahh yea that would do it :P ... glad you figured it out! – DBoyer Jun 24 '15 at 02:42
  • 1
    Thanks for the followup! This was my issue as well. I had a tap gesture to dismiss the keyboard. Once I commented that out tapping the cell worked like a champ. – Ponyboy Jul 27 '16 at 03:10
1

This should work but it would be much cleaner to make a custom cell subclass.

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")

    cell.textLabel!.text = "Test"

    let backgroundView = UIView()
    backgroundView.backgroundColor = YOUR_COLOR
    cell.selectedBackgroundView = backgroundView

    return cell
}

Remove because we want cell selection

cell.selectionStyle = UITableViewCellSelectionStyle.None

Remove since this is already taken care of in the cell subclass

override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
}

override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
}
DBoyer
  • 3,062
  • 4
  • 22
  • 32
  • I currently am using a default cell, not a custom cell. Will this require a custom cell? Also, maybe I am mis-reading this, but it looks like this will change the color of the cell immediately upon the creation of the cell. This won't change it upon cell selection by clicking on the cell will it? – Jason247 Jun 24 '15 at 01:58
  • This sets the cell's "selectedBackgroundVIew" not the "backgroundView" so it only appears when highlighted and selected...I will edit my answer to help you – DBoyer Jun 24 '15 at 02:03
  • Ok, you're right that it does change the color when the cell is selected, but I am still having the same problem that I had when using didSelectRowAtIndexPath. I have to hold the cell for 3 seconds for the cell to remain highlighted. – Jason247 Jun 24 '15 at 02:09
  • did you remove/comment out all the code you have for didHighlightRowAtIndexPath and didUnhighlightRowAtIndexPath and didSelectRowAtIndexPath? .... cell selection messes with the background color of all subviews by itself so that could be messing with it – DBoyer Jun 24 '15 at 02:11
  • Yes, that has been commented out. – Jason247 Jun 24 '15 at 02:13
  • Last things I can see is cell.selectionStyle = UITableViewCellSelectionStyle.None you can ditch and shouldHighlightRowAtIndexPath is unnecessary if you are always going to return true – DBoyer Jun 24 '15 at 02:14
  • Unfortunately removing those had no affect as well. Is this normal behavior for selecting a cell? My gut says this isn't and perhaps there is something wrong with my code somewhere, but I have no idea where. – Jason247 Jun 24 '15 at 02:17
0

I don't have enough reputation points to reply to @Jason247, so hence typing it as an answer. He's right. I had the same issue with a delay of the cells registering a click. Commented out the UITapGestureRecognizer and the delay went away.

In my context, I was using a UISearchBar. I replaced my UITapGestureRecognizer with the search bar's optional delegate method:

class ViewController: UIViewController, UISearchBarDelegate{

    func searchBarSearchButtonClicked(searchBar: UISearchBar)
    {
        self.mySearchBar.endEditing(true)
    }
}

You can find more solutions for dismissing the keyboard when using a UISearchBar here

Plaz
  • 21
  • 3