我有自定义单元格的表视图,它有按钮我希望当用户点击该按钮时,按钮的图像应该改变任何想法如何做到这一点因为我希望在另一种方法中更改cellImage按钮图像
[cell.theSyncButton addTarget:self action:@selector(syncButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.theSyncButton setTag:indexPath.row];
方法:
-(IBAction)syncButtonAction:(id)sender{
// I want to change the cell.SyncButton image here
}
任何想法如何做到这一点。
答案 0 :(得分:2)
[btnSync setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[btnSync setImage:[UIImage imageNamed:@"refresh-active.png"] forState:UIControlStateHighlighted];
答案 1 :(得分:2)
-(IBAction)syncButtonAction:(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:img forState:UIControlStateSelected];
}
答案 2 :(得分:0)
试试这个..
-(IBAction)syncButtonAction:(id)sender
{
UIButton * syncButton = (UIButton *) sender;
[syncButton setBackgroundImage: <NormalImage.png>
forState: UIControlStateNormal];
[syncButton setBackgroundImage: <HighlightedImage.png
forState: UIControlStateHighlighted];
}
答案 3 :(得分:0)
您需要做的就是代替所有代码:
UIImage *img = [UIImage imageNamed:@"yourImage.png"];
[cell.theSyncButton setImage:img forState:UIControlStateSelected];
答案 4 :(得分:0)
这是我的解决方案: 我使用UIImageVIew作为按钮
例如,starIcon是一个UIImageView,我将其添加到我的自定义UITableViewCell按钮中。 和ArticleCell是我的自定义UITableViewCell类。
在UITableView dataSource方法 cellForRowAtIndexPath :
ArticleCell* cell = [tableView dequeueReusableCellWithIdentifier:@"reused"];
if (cell==nil) {
NSLog(@"init tableViewCell");
cell = [[ArticleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reused"];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonCallback:)];
gesture.numberOfTapsRequired=1;
gesture.numberOfTouchesRequired=1;
[cell.starIcon addGestureRecognizer:gesture];
cell.starIcon.userInteractionEnabled=YES;
NSLog(@"init tableViewCell end");
}
确保设置 userInteractionEnabled == true;
然后,在buttonCallback方法中,您可以通过
获取UIimageView -(void)buttonCallback:(UIGestureRecognizer*)gestureRecognizer{
UIImageView* star = (UIImageView*)[gestureRecognizer view];
}
所以,你可以改变你的imageView。
答案 5 :(得分:0)
-(IBAction)syncButtonAction:(id)sender{
[button setImage:[UIImage imageNamed:@"yourimage.png"] forState:UIControlStateNormal];
}