[英]Change Image of UIButton in UITableView Cell Dynamically
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
{
}
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];
}
}
In that bbtnCheck you have to get the instance of that tableView (ie 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];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.