Jason247
Jason247

Reputation: 1024

Swift: Delay in UITableViewCell Selection

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
    }
}

Upvotes: 0

Views: 2622

Answers (3)

Plaz
Plaz

Reputation: 31

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

Upvotes: 0

Jason247
Jason247

Reputation: 1024

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.

Upvotes: 2

DBoyer
DBoyer

Reputation: 3122

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()
}

Upvotes: 1

Related Questions