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

如何在iphone中单击更改tableView单元格按钮,iphone,ios,uitableview,Iphone,Ios,Uitableview,我有一个带有自定义单元格的表视图,它有一个按钮,我想当用户点击该按钮时,按钮的图像应该会改变。你知道怎么做吗?因为我想用另一种方法改变cellImage按钮的图像 [cell.theSyncButton addTarget:self action:@selector(syncButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.theSyncButton setTag:indexPath.row];

我有一个带有自定义单元格的表视图,它有一个按钮,我想当用户点击该按钮时,按钮的图像应该会改变。你知道怎么做吗?因为我想用另一种方法改变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 
}
你知道怎么做吗。

试试这个

-(IBAction)syncButtonAction:(id)sender
{
    UIButton * syncButton = (UIButton *) sender;
    [syncButton setBackgroundImage: <NormalImage.png>
                          forState: UIControlStateNormal];
    [syncButton setBackgroundImage: <HighlightedImage.png
                          forState: UIControlStateHighlighted];
}
-(iAction)同步按钮操作:(id)发送方
{
UIButton*同步按钮=(UIButton*)发送方;
[同步按钮设置背景图像:
forState:uicontrol状态正常];
[syncButton setBackgroundImage:试试这个

-(IBAction)syncButtonAction:(id)sender
{
    UIButton * syncButton = (UIButton *) sender;
    [syncButton setBackgroundImage: <NormalImage.png>
                          forState: UIControlStateNormal];
    [syncButton setBackgroundImage: <HighlightedImage.png
                          forState: UIControlStateHighlighted];
}
-(iAction)同步按钮操作:(id)发送方
{
UIButton*同步按钮=(UIButton*)发送方;
[同步按钮设置背景图像:
forState:uicontrol状态正常];

[syncButton setBackgroundImage:您需要做的不是所有代码,而是:

  UIImage *img = [UIImage imageNamed:@"yourImage.png"];
  [cell.theSyncButton setImage:img forState:UIControlStateSelected];

您需要做的不是所有代码,而是:

  UIImage *img = [UIImage imageNamed:@"yourImage.png"];
  [cell.theSyncButton setImage:img forState:UIControlStateSelected];
以下是我的解决方案: 我使用UIImageVIew作为按钮

例如,starIcon是一个UIImageView,我将其作为按钮添加到自定义UITableViewCell中。 ArticleCell是我自定义的UITableViewCell类

在UITableView数据源方法中,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方法中,可以通过

    -(void)buttonCallback:(UIGestureRecognizer*)gestureRecognizer{
       UIImageView* star = (UIImageView*)[gestureRecognizer view];
    }
因此,您可以更改图像视图。

以下是我的解决方案:
-(IBAction)syncButtonAction:(id)sender{
        [button setImage:[UIImage imageNamed:@"yourimage.png"] forState:UIControlStateNormal];

}
我使用UIImageVIew作为按钮

例如,starIcon是一个UIImageView,我将其作为按钮添加到自定义UITableViewCell中。 ArticleCell是我自定义的UITableViewCell类

在UITableView数据源方法中,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方法中,可以通过

    -(void)buttonCallback:(UIGestureRecognizer*)gestureRecognizer{
       UIImageView* star = (UIImageView*)[gestureRecognizer view];
    }


因此,您可以更改图像视图。

cell将如何在另一种方法中调用这意味着当按钮进入选择性模式时,它将更改为您的图像。(当用户单击按钮时进入选择性模式)是,它应该这样做,然后尝试uicontrol状态高亮显示按Pradhyuman Chavda的回答建议使用uicontrol状态高亮显示。单元格将如何调用其他方法这意味着当按钮进入选择模式时,它将更改为您的图像。(当用户单击按钮时进入选择模式)是,它应该这样做,然后尝试uicontrol状态高亮显示按Pradhyuman Chavda的回答建议使用uicontrol状态高亮显示。在按下按钮
-(IBAction)syncButtonAction:(id)后是否调用此方法发送者
yes这是在按钮操作时调用的方法,如在
syncButtonAction
scroll tableView中更改按钮图像后的代码所示。在滚动整个表格后,是否有更改的图像仍然显示相同。按下按钮后是否调用此方法
-(iAction)syncButtonAction:(id)发件人
yes这是在按钮操作中调用的方法,如在
syncButtonAction
scroll tableView中更改按钮图像后的代码所示。是否有更改的图像在滚动整个表格后仍保持不变?按钮是否在tableView单元格内?按钮是否在tableView单元格内??
-(IBAction)syncButtonAction:(id)sender{
        [button setImage:[UIImage imageNamed:@"yourimage.png"] forState:UIControlStateNormal];

}