1

I'm trying create TableViewCells which mimic buttons. This means that on touch down, there should be a highlight effect and on touch up should trigger the standard selected state. This works as intended, but the problem is that there is a split second delay between touch down and the highlighted state appearing. Why is this? How can I make the highlight appear immediately on touch down without the delay?

Here's the code I'm using on my TableView delegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setSelected:YES animated:NO];
    // do something here
    [[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:NO];
}
Halle
  • 3,584
  • 1
  • 37
  • 53
zakdances
  • 22,285
  • 32
  • 102
  • 173
  • Could we see the code for the subclassed `UITableViewCell`? Particularly the part associated with highlighting. – sooper May 04 '12 at 23:19
  • @sooper I'm not subclassing UITableViewCell. The default class is working fine, except for this issue. Should I be subclassing? What should I be adding? – zakdances May 04 '12 at 23:23
  • I am having the same problem, it waits like 1/3 of a second before setting the cell highlighted – Yunus Nedim Mehel Aug 01 '13 at 12:51

1 Answers1

0

I suppose I don't really understand your question as you've asked it. UITableViewCell already acts "like a button" when pressed in the sense of it highlighting. From the looks of your code, it really does nothing that the tableView doesn't do natively.

Basically, the reason you are seeing a delay is because the cell already highlights on touch, and what you are doing is setting Selected to YES, then NO, but the cell already does this, so it's kind of doing the same thing twice, once on it's own, then once forced — this is the reason for the delay you are seeing.

The only thing that should go in didSelectRowAtIndexPath: is the actions you want to happen when the button is pressed, NOT what you want the cell to do or how it should behave upon being touched. There are other delegate methods that would handle these behaviors.

If you are looking to change the highlight color of the cell, then see my question/answer here.

Update

By default, the UITableView code provided by Apple does not contain the deselect method. So when you select the cell it stays selected. To deselect the cell, add the following method to didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    /* The following will deselect the cell on touchUp */
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}
Community
  • 1
  • 1
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • When I say "button" I mean the highlighted state should turn on (on touch down) then turn off (on touch up). The default behavior for UITableViewCell is that it turns on (on touch down) then STAYS on (on touch up) to show that it is selected. But I now feel that this is a red herring because I've tried deleting my 2 "Set Selected" statements and the delay remains. So even with a completely empty didSelectRowAtIndexPath method, there still is a slight delay for the highlight to appear when touch down occurs. Is this observable to you or do you see no delay? – zakdances May 05 '12 at 00:08
  • Ah, now your question is more clear. See the **Update** in my answer. – WrightsCS May 05 '12 at 00:15
  • I didn't know about the deselectRowAtIndexPath method, thank you. That super annoying delay is still there though. It might be a hard coded delay in the source code for UITableView...I'll see if I can see anything. – zakdances May 05 '12 at 00:24
  • There's something in the UITableViewCell m file called "NSTimer *_deselectTimer;" that might be related to the delay. – zakdances May 05 '12 at 00:26
  • There is nothing "hard coded" in the tableView that makes the delay. You need to remove your `[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:NO];` methods and just keep the method I mentioned. You also may have something delaying the loading of your next view which would cause a delay as well, but that has nothing to do with the tableView. – WrightsCS May 05 '12 at 01:28
  • My above comment mentioned that I tried it without the setSelected statements. I've also tried it with your deselectRowAtIndexPath method. I don't have the tables or cells wired up to push any views or do anything. – zakdances May 05 '12 at 01:45
  • Post more code then because you may have something else that is hijacking the table. – WrightsCS May 05 '12 at 01:50