[英]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.