0

I have a UITableViewCell with UIButton in each cell. When the button is pressed I set its state to highlighted like in this answer:

[self performSelector:@selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2] afterDelay:0.0];

Then I do this:

- (void)doHighlight:(UIButton *)sender {

    if (sender.highlighted) {
        [sender setHighlighted:NO];
    } else {
    [sender setHighlighted:YES];
    }
}

But the button not only is just not highlighted at all, not speaking about the fact that I should be able to UNhighlight it.

Any ideas on what is wrong?

Community
  • 1
  • 1
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • Are u sure that [cell.subviews objectAtIndex:2] a UIButton? – HRM Feb 07 '13 at 14:05
  • From which function you are calling this `performSelector`. It is mentioned in the post that, when the button is pressed, in that case how you get the `cell` instance? – HRM Feb 07 '13 at 14:12
  • @HRM from the function which is assigned to be the one when the button is pressed, an ordinary `target:self selector:@selector(copyPressedFromCell:)` and I create a button in `tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath` – Sergey Grischyov Feb 07 '13 at 14:23
  • So, y u taking button from cell. You shoud get the button in copyPressedFromCell,rt? Also, I dnt knw, whether u creating the button in willDisplayCell causing any issues.. – HRM Feb 07 '13 at 15:00
  • @HRM the problem here is not with the Button itself, but either with `delay` of which I can't get rid of, or it's with the `if` statement in the `doHighlight` method. – Sergey Grischyov Feb 07 '13 at 15:04

2 Answers2

1

I ended up using UIButton's selected property. It does not require any delay and it works brilliantly with this type of things:

if (!sender.selected) {
    [sender setSelected:YES];
    [cell addSubview:hiddenButton];
    [self performSelector:@selector(doHighlight:) withObject:sender];
} else {
    [sender setSelected:NO];
    [self performSelector:@selector(doUnHighlight:) withObject:sender];
}
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
0

Here is a possible solution you are looking for. I have modified my answer

 UIButton *button=[cell.subviews objectAtIndex:2];

//I am adding these five lines to ensure the different 
//states of the button to achieve your highlighted state goal. 

UIImage *highlightImage = [UIImage imageNamed:@"highlight.png"];//Also used  when button is selected
UIImage *normalImage = [UIImage imageNamed:@"normal.png"];
[button setBackgroundImage:normalImage forState:(UIControlStateHighlighted)];
[button setBackgroundImage:highlightImage forState:(UIControlStateSelected)];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];

 [self performSelector:@selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2]];

-(IBAction) doHighlight:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *btn=(UIButton*)sender;
        if (btn.isSelected) {
            [btn setSelected:NO];
        }
        else
        {
            [btn setSelected:YES];
        }
    }
}
Rahul
  • 509
  • 2
  • 4
  • 22
  • Unfortunately, you are wrong.There IS a big sense in using that `delay` thing. Without it, the button won't be highlighted until it's tapped again. And even if I use the `delay` with your decision it hives me nothing. – Sergey Grischyov Feb 07 '13 at 14:17