[英]how to change tableView Cell button on click in iphone
I have table view with custom cell and it has button i want that when user click on that button then the image of button should be changes any idea how to do this becuase i want that cellImage button image to be changed in another method. 我有自定义单元格的表格视图,它有按钮我希望当用户点击该按钮时,按钮的图像应该更改任何想法如何做到这一点因为我希望在另一种方法中更改cellImage按钮图像。
[cell.theSyncButton addTarget:self action:@selector(syncButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.theSyncButton setTag:indexPath.row];
method: 方法:
-(IBAction)syncButtonAction:(id)sender{
// I want to change the cell.SyncButton image here
}
any idea how to do this. 任何想法如何做到这一点。
[btnSync setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[btnSync setImage:[UIImage imageNamed:@"refresh-active.png"] forState:UIControlStateHighlighted];
-(IBAction)syncButtonAction:(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:img forState:UIControlStateSelected];
}
try this.. 尝试这个..
-(IBAction)syncButtonAction:(id)sender
{
UIButton * syncButton = (UIButton *) sender;
[syncButton setBackgroundImage: <NormalImage.png>
forState: UIControlStateNormal];
[syncButton setBackgroundImage: <HighlightedImage.png
forState: UIControlStateHighlighted];
}
Instead of all that code you have all you have to do is: 您需要做的就是完成所有代码而不是所有代码:
UIImage *img = [UIImage imageNamed:@"yourImage.png"];
[cell.theSyncButton setImage:img forState:UIControlStateSelected];
Here is my solution: i use UIImageVIew as button 这是我的解决方案:我使用UIImageVIew作为按钮
for example , the starIcon is an UIImageView ,which i add into my custom UITableViewCell as button. 例如,starIcon是一个UIImageView,我将其作为按钮添加到我的自定义UITableViewCell中。 and ArticleCell is my custom UITableViewCell class.
和ArticleCell是我的自定义UITableViewCell类。
in UITableView dataSource method cellForRowAtIndexPath : 在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");
}
make sure you set userInteractionEnabled == true; 确保你设置userInteractionEnabled == true;
then , in your buttonCallback method ,you can get UIimageView by 然后,在你的buttonCallback方法中,你可以获得UIimageView
-(void)buttonCallback:(UIGestureRecognizer*)gestureRecognizer{
UIImageView* star = (UIImageView*)[gestureRecognizer view];
}
so , you can change your imageView. 所以,你可以改变你的imageView。
-(IBAction)syncButtonAction:(id)sender{
[button setImage:[UIImage imageNamed:@"yourimage.png"] forState:UIControlStateNormal];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.