开发者

Communicating between UITableViewCells in a UITableViewController

I know this is a common question topic. However I haven't found an answer after extensive searching so I'm asking it here.

I have a UITableViewController where each row is an instance of a UITableViewCell subclass. Each UITableViewCell subclass has a UIButton. Initially all UIButtons have th开发者_Go百科e same image (just a blue circle). Tapping a button causes only that button's image to change (say to a red circle). This part is easy: just handle the tap inside the UITableViewCell subclass and toggle the image.

Here's the challenging part: when I tap another blue button I want the button that's currently red (if any) to toggle it's image back to blue. How can I tell this button to toggle it's image?

Where do I keep track of the button that's currently red?


Implement the - (void)setSelected:(BOOL)selected animated:(BOOL)animated method in your UITableViewCell subclass. When the cell is selected, make the UIButton image red; otherwise, set the UIButton image to blue. This method is called whenever a new cell is selected, so there will always be only one red-button cell.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected)
    {
        [myButton setImage:RED_IMAGE forState:UIControlStateNormal]; //or however else you want to change your button's image
    }else
    {
        [myButton setImage:BLUE_IMAGE forState:UIControlStateNormal];
    }

    [super setSelected:selected animated:animated];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜