开发者

uibutton image doesnt change when cell gets highlighted

I have a custom UIbutton in every cell of my tableview. This UIbutton works like the Add To Favs functionality of STAR button used in Gmail,etc. Means whenever i click on that button the appropriate item gets added to favs list and button's image gets changed to glowing star image. On clicking it again, that item gets removed from the favs list and a empty star is used as its image.

The problem i am facing here is that when i select the cell, it always shows the empty image of the button and not the highlighted star image.

note that this problem gets solved if the separator style of tableview is set to NONE. But i do want the separator style.

plz 开发者_JAVA技巧help if you can.


Used this code for changing the button image in the normal and selected state. If you want to add the favorite list, so you need to get the button index value .

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    customBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    customBtn.frame = CGRectMake(292, 8.0, 30, 28); 

    UIImage *likeSelectedImage = [UIImage imageNamed:@"star off.png"];

   [customBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];

    [customBtn addTarget:self action:@selector(addToFavouriteClicked1:) forControlEvents:UIControlEventTouchUpInside]; 
    customBtn.tag = indexPath.row; 
    [cell.contentView addSubview:customBtn];
}


-(void) addToFavouriteClicked1 : (id) sender
{
    UIImage *likeSelectedImg = [UIImage imageNamed:@"star on.png"];

    UIImage *likeImg = [UIImage imageNamed:@"star off.png"];

    if ([sender isSelected]) {
        [sender setImage:likeImg forState:UIControlStateNormal];
        [sender setSelected:NO];
    }else {
        [sender setImage:likeSelectedImg forState:UIControlStateSelected];
        [sender setSelected:YES];
    } 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜