如何检查UITableview中的UIButtons的条件

按钮1的条件按钮3 我在表格视图中有三个button图像,我想检查它们之间的条件。 当我点击button1意味着背景图像变成蓝色。 同时我点击button1将移动到正常状态的白色。 相同的另外两个button。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath![button3 condition][3] { static NSString *cellIdentifier = @"HistoryCell"; CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } UIButton *button1; button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(80, 27, 36, 36); [button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal]; button1.tag = 1; [button1 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside]; [button1 setSelected:true]; [cell.contentView addSubview:button1]; UIButton *button2; button2 = [UIButton buttonWithType:UIButtonTypeCustom]; button2.frame = CGRectMake(160, 27, 36, 36); [button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal]; button2.tag = 1; [button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside]; [button2 setSelected:true]; [cell.contentView addSubview:button2]; UIButton *button3; button3 = [UIButton buttonWithType:UIButtonTypeCustom]; button3.frame = CGRectMake(240, 27, 36, 36); [button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal]; button3.tag = 1; [button3 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside]; [button3 setSelected:true]; [cell.contentView addSubview:button3]; return cell; } 

我的情况是:button1被点击意味着button3不应该改变。 当button3被点击意味着button1不应该改变。button2可以在两种情况下select。

 - (void)radiobtn:(UIButton *)button { if(button.tag == 1) { [button setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected]; } if(button.tag == 2) { [button setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected]; } if(button.tag == 3) { [button setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected]; } } 

任何人都可以帮助我编码。

首先你可以为每个button设置不同的标签

  button.tag == 1, button.tag == 2, button.tag == 3 

然后,你可以这样写你的radiobtn行动..

 -(IBAction) radiobtn:(id)sender { UIButton *yourBtn = (UIButton *)[self.view viewWithTag:[sender tag]]; if(yourBtn.tag == 1) { [yourBtn setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected]; } else if(yourBtn.tag == 2){ [yourBtn setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected]; } else{ [yourBtn setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected]; } } 
 button1.tag=1; button2.tag=2; button3.tag=3; -(void)radiobtn:(UIButton *)button { if(button.tag==1) { if(![button3 isSelected]) { if([button1 isSelected]) button1.backgroundColor = [UIColor blueColor]; else button1.backgroundColor = [UIColor whiteColor]; } else { //No change } } else if(button.tag==2) { if([button2 isSelected]) button2.backgroundColor = [UIColor blueColor]; else button2.backgroundColor = [UIColor whiteColor]; } else if(button.tag==3) { if(![button1 isSelected]) { if([button3 isSelected]) button3.backgroundColor = [UIColor blueColor]; else button3.backgroundColor = [UIColor whiteColor]; } else { //No change } } } 

你能试试吗

请检查!! 我在我的代码上做了同样的事情。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"UITableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; } UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; [selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here [cell setSelectedBackgroundView:selectedBackgroundView]; self.tableview.separatorColor = [UIColor whiteColor]; if (_userResult.relationStatus == [arrAnswrs objectAtIndex:indexPath.row]){ cell.accessoryType=UITableViewCellAccessoryCheckmark; }else if (_userResult.childrenStatus == [arrAnswrs objectAtIndex:indexPath.row]){ cell.accessoryType=UITableViewCellAccessoryCheckmark; } else{ cell.accessoryType=UITableViewCellAccessoryNone; } [[UITableViewCell appearance] setTintColor:[UIColor redColor]]; cell.textLabel.text = [arrAnswrs objectAtIndex:indexPath.row]; cell.textLabel.font = [UIFont fontWithName:@"Roboto" size:16]; cell.textLabel.textColor = UIColorFromRGB(0xe212121); cell.backgroundColor = UIColorFromRGB(0xeaaea7); return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"Select indexPath.section %d --- index %d", (int)indexPath.section, (int)indexPath.row); [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSString *status = [arrAnswrs objectAtIndex:indexPath.row]; UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor redColor]; [cell setSelectedBackgroundView:bgColorView]; int iSeletedButton = (int)selectButton.tag ; NSLog(@"value %@ -- iSeletedButton %d", [arrAnswrs objectAtIndex:indexPath.row], iSeletedButton); switch (iSeletedButton) { case 1: _userResult.relationStatus = status; _labelFrstStatus.text = status; _labelFrstStatus.textColor = [UIColor blackColor]; break; case 2: _userResult.childrenStatus = status; _labelSecndStatus.text = status; _labelSecndStatus.textColor = [UIColor blackColor]; break; default: break; } if (_userResult.relationStatus || _userResult.childrenStatus) { cell.accessoryType = UITableViewCellAccessoryNone; } else cell.accessoryType = UITableViewCellAccessoryCheckmark; [tableView reloadData]; } 

你可以设置标签button

 button1.tag=1; button2.tag=2; button3.tag=3; //instead of tag 1 for all buttons 

全局参考button1,button2,button3的button。

 NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCellScrollView NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableViewCell - (void)radiobtn:(UIButton *)button { if(button.tag==1) { //handle CustomizedCellView * cell = (CustomizedCellView*)[[[button superview]superview]superview]; if ([button.imageView.image isEqual:[UIImage imageNamed:@"lblue.png"]]) { [button setImage:[UIImage imageNamed:@"lwhite.png"] forState:UIControlStateSelected]; } for (UIButton *btn in cell.contentView.subviews) { if (btn.tag==3) { [btn setImage:[UIImage imageNamed:@"vwhite.png"] forState:UIControlStateSelected]; } } } else if(button.tag==2) { //handle if ([button.imageView.image isEqual:[UIImage imageNamed:@"eblue.png"]]) { [button setImage:[UIImage imageNamed:@"ewhite.png"] forState:UIControlStateSelected]; } } else if(button.tag==3) { //handle CustomizedCellView * cell = (CustomizedCellView*)[[[sender superview]superview]superview]; if ([button.imageView.image isEqual:[UIImage imageNamed:@"vblue.png"]]) { [button setImage:[UIImage imageNamed:@"vwhite.png"] forState:UIControlStateSelected]; } for (UIButton *btn in cell.contentView.subviews) { if (btn.tag==2) { [btn setImage:[UIImage imageNamed:@"lwhite.png"] forState:UIControlStateSelected]; } } } } 

希望它可以帮助你…!