我有一个带按钮的costoum单元格。我希望能够在按下按钮时更改按钮的图像。我正在尝试这个,但我没有得到理想的结果。
in cellForRowAtIndexPath:
BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]];
if (isTheObjectThere==TRUE) {
cell.favBtn.hidden = NO;
[cell.favBtn setImage:[UIImage imageNamed:@"fav_selected.png"] forState:UIControlStateNormal];
cell.favBtn.tag = indexPath.row;
[cell.favBtn addTarget:self action:@selector(favoriteBtn:) forControlEvents:UIControlEventTouchUpInside];}
else {
cell.favBtn.hidden = NO;
[cell.favBtn setImage:[UIImage imageNamed:@"Favourites_Green@3x.png"] forState:UIControlStateNormal];
cell.favBtn.tag = indexPath.row;
[cell.favBtn addTarget:self action:@selector(unfavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
}
和:
-(void)favoriteBtn:(UIButton*)sender
{
if ([cell.favBtn.currentImage isEqual:[UIImage imageNamed:@"fav_selected.png"]]){
[cell.favBtn setImage:[UIImage imageNamed:@"Favourites_Green@3x.png"] forState:UIControlStateNormal];
}
else if ([cell.favBtn.currentImage isEqual:[UIImage imageNamed:@"Favourites_Green@3x.png"]]){
[cell.favBtn setImage:[UIImage imageNamed:@"fav_selected.png"] forState:UIControlStateNormal];
}
答案 0 :(得分:1)
应该类似于以下内容:
cellForRow :中的
[cell.favButton addTarget:self action:@selector(favButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
favButtonTapped :中的
if ([self.favoriteArray containsObject:indexPath]) {
[self.favoriteArray removeObject:indexPath];
// sender setImage...
} else {
[self.favoriteArray addObject:indexPath];
// sender setImage...
}
祝你好运:)