动态更改UITableView单元中UIButton的图像

时间:2023-02-10 21:08:06

I add UIButton in the cell Dynamically and change UIButton image on click of UIBarButtonItem how i can change?

我动态地在单元格中添加UIButton并在点击UIBarButtonItem时更改UIButton图像我如何更改?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Display"] ; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@" ,[arrItemList objectAtIndex:indexPath.row]]];

    UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnCheck.frame = CGRectMake(6.0f, 2.0f, 32.0f, 25.0f);
    [btnCheck setTitle:@"" forState:UIControlStateNormal]; 
    [btnCheck addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnCheck]; 

    return cell;
}
//Here IBAction for Bar Button Item
//when ever user click on BarButton then set the image of all btnCheck
-(IBAction)bbtnCheck:(id)sender
{
}

2 个解决方案

#1


1  

Assuming that your question is how to change the image of a button w/in every cell of your table when a bar button is clicked, I suggest the following:

假设您的问题是如何在单击条形按钮时更改桌面每个单元格中按钮的图像,我建议如下:

Within tableView:cellForRowAtIndexPath, add:

在tableView:cellForRowAtIndexPath中,添加:

btnCheck.tag = MY_BUTTON_TAG_NUMBER;

Then, when the bar button item is clicked, loop through the visible cells and use the button's tag to identify it and update its image.

然后,当单击条形按钮项目时,循环显示可见单元格并使用按钮的标记来识别它并更新其图像。

-(IBAction)bbtnCheck:(id)sender {
 for (UITableViewCell *cell in self.tableView.visibleCells) {
   UIButton *button = [cell viewWithTag:MY_BUTTON_TAG_NUMBER];
   [button setImage:[UIImage imageNamed:SOME_NEW_IMAGE] forState:UIControlStateNormal];
  }
}

#2


0  

In that bbtnCheck you have to get the instance of that tableView (i.e. particular cell)..Then after you can get buttons added on that particular cell.. Please Check this....

在那个bbtnCheck你必须获得该tableView的实例(即特定的单元格)..然后你可以在该特定单元格上添加按钮..请检查这....

-(IBAction)bbtnCheck:(id)sender
{
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:YourRow inSection:YourSection]; // you have to set Row and Section according to your requirement
     UITableViewCell *cellNew = [self.tblComments cellForRowAtIndexPath:indexpath];
    for (UIButton *btn in [cellNew.contentView subviews]) 
    {
        if ([btn isKindOfClass:[UIButton class]]) 
        {
            [btn setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        }
    }    
}

#1


1  

Assuming that your question is how to change the image of a button w/in every cell of your table when a bar button is clicked, I suggest the following:

假设您的问题是如何在单击条形按钮时更改桌面每个单元格中按钮的图像,我建议如下:

Within tableView:cellForRowAtIndexPath, add:

在tableView:cellForRowAtIndexPath中,添加:

btnCheck.tag = MY_BUTTON_TAG_NUMBER;

Then, when the bar button item is clicked, loop through the visible cells and use the button's tag to identify it and update its image.

然后,当单击条形按钮项目时,循环显示可见单元格并使用按钮的标记来识别它并更新其图像。

-(IBAction)bbtnCheck:(id)sender {
 for (UITableViewCell *cell in self.tableView.visibleCells) {
   UIButton *button = [cell viewWithTag:MY_BUTTON_TAG_NUMBER];
   [button setImage:[UIImage imageNamed:SOME_NEW_IMAGE] forState:UIControlStateNormal];
  }
}

#2


0  

In that bbtnCheck you have to get the instance of that tableView (i.e. particular cell)..Then after you can get buttons added on that particular cell.. Please Check this....

在那个bbtnCheck你必须获得该tableView的实例(即特定的单元格)..然后你可以在该特定单元格上添加按钮..请检查这....

-(IBAction)bbtnCheck:(id)sender
{
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:YourRow inSection:YourSection]; // you have to set Row and Section according to your requirement
     UITableViewCell *cellNew = [self.tblComments cellForRowAtIndexPath:indexpath];
    for (UIButton *btn in [cellNew.contentView subviews]) 
    {
        if ([btn isKindOfClass:[UIButton class]]) 
        {
            [btn setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        }
    }    
}