How can I change the image of a button when tapping on another button in another tab
我有一个
非常感谢。
这是我在tab1中创建表视图的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; UIButton *market= [UIButton buttonWithType:UIButtonTypeCustom]; [market setFrame:CGRectMake(200, 6, 30, 30)]; market.tag = 4000; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [cell.contentView addSubview:market]; UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)]; pricelabel.backgroundColor = [UIColor clearColor]; pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16]; pricelabel.font = [UIFont boldSystemFontOfSize:16]; pricelabel.textColor = [UIColor darkGrayColor]; pricelabel.tag = 3000; pricelabel.textAlignment = NSTextAlignmentRight; [cell.contentView addSubview: pricelabel]; [pricelabel release]; } UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; [marketButton setTag:indexPath.row]; if([sellingArray count]>0) { if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing { [marketButton setSelected:NO]; [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace { [marketButton setSelected:YES]; [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } } [market setTag:indexPath.row]; if([priceNewArray count]> 0) { UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000]; pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]]; if ([sellingArray count]>0) { if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) { pricelbl.hidden = NO; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) { pricelbl.hidden = YES; } } } return cell; } |
我尝试使用[tableview reloadData],但它仅重新加载UILabel,当我比较它们时,UIButton不会更改图像。
您可以使用
在您的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateBtn:) name:@"updateBtn" object:nil]; [super viewDidLoad]; } -(void)UpdateBtn:(NSNotification *)notification { //Update your button image code here } |
现在,您只需要调用此方法即可。在您的
1 | [[NSNotificationCenter defaultCenter] postNotificationName:@"updateBtn" object:self]; |
希望对您有帮助:)