【问题标题】:how to check the condition for UIButtons in UITableview如何在 UITableview 中检查 UIButtons 的条件
【发布时间】:2015-02-20 06:13:27
【问题描述】:

我在表格视图中有三个按钮图像,我想检查它们之间的条件。当我单击按钮 1 表示背景图像变为蓝色。同时我点击按钮 1 它将变为正常状态的白色。另外两个按钮也一样。

- (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;
}

我的条件是:当按钮 1 被点击意味着按钮 3 不应该改变。当按钮 3 被点击意味着按钮 1 不应该改变。按钮 2 可以在这两种情况下选择。

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

谁能帮我写代码。

【问题讨论】:

  • 这只是一个简单的逻辑,你应该自己做。
  • 您希望 button2 在 button1 或 button2 发生变化时发生变化吗?
  • @jssmeet singh 按钮 1 被点击意味着按钮 3 不应该改变。当按钮 3 被点击意味着按钮 1 不应该改变。按钮 2 可以在这两种情况下选择。

标签: ios objective-c iphone uitableview uibutton


【解决方案1】:

首先你可以为每个按钮的like设置不同的标签

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

然后你可以像这样写你的radiobtn Action..

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

【讨论】:

    【解决方案2】:
    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
         }
    }
    }
    

    你可以试试这个吗..

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你可以给按钮设置标签

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

        全局引用按钮为 button1,button2,button3..

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

        希望对您有所帮助...!

        【讨论】:

        • 你是否使用了我的代码..我的意思是 CustomizedCellView * cell = (CustomizedCellView*)[button superview];.. 如上所述?
        • 现在检查..一旦调试代码...并打印 Cell 和 cell.contentView.subviews..并检查
        猜你喜欢
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 2017-04-16
        相关资源
        最近更新 更多