使用此代碼可以在正常狀態和選定狀態下更改按鈕圖像。如果您想添加收藏夾列表,那麼您需要獲取按鈕索引值。
- (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];
}
}
您能否爲我們提供您的'cellForRow'方法實現。解決問題會更容易。 – RaffAl 2011-03-16 11:45:49
嗨,MJ你投票了嗎? – 2011-05-27 13:11:58