简体   繁体   English

如何在iphone上点击更改tableView单元格按钮

[英]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.

相关问题 在tableview手机iphone中添加按钮 - add a button in a tableview cell iphone iPhone-触摸时如何更改表格视图的单元格高度 - iPhone - How to change the cell height of a tableview when touches it 如何在iPhone的组TableView中更改所选单元格背景图像 - how to change cell background image on selected in group tableview in iphone 如何在iPhone的“按钮单击”下拉列表中显示表格视图? - How to display the tableview in dropdown list on Button Click in iPhone? 如何在iPhone SDK中单击按钮以清空整个tableview? - How to empty out the whole tableview on a button click in iPhone SDK? 如何在iPhone的左侧控制器的表格视图行上单击以更改中心视图?(MFSideMenu) - How to change centerview on click of tableview row of leftsidecontroller in iphone?(MFSideMenu) 如何在单击按钮时获取tableview单元格中textfield的值? - How to get the value of textfield in a tableview cell on click of a button? 在didSelectRowAtIndexPath上更改iPhone tableview单元格标签文本 - change iPhone tableview cell label text on didSelectRowAtIndexPath 如何通过iPhone中的tableview的indexpath传递更改切换按钮? - How to pass change the toggle button through indexpath of tableview in iPhone? 如何在下一个按钮上更改图像单击+ iphone - how to change image on next button click +iphone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM