繁体   English   中英

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

[英]Change Image of UIButton in UITableView Cell Dynamically

我动态地在单元格中添加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
{
}

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

在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];
  }
}

在那个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];
        }
    }    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM