3

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

- (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
{
}
4

2 回答 2

1

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

在 tableView:cellForRowAtIndexPath 中,添加:

btnCheck.tag = MY_BUTTON_TAG_NUMBER;

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

-(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];
  }
}
于 2013-01-01T09:19:44.183 回答
0

在那个 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];
        }
    }    
}
于 2013-01-01T08:53:59.880 回答