【问题标题】:How to create two radio buttons on a uitableview cell如何在 uitableview 单元格上创建两个单选按钮
【发布时间】:2015-08-04 13:23:32
【问题描述】:

我在一个表格视图单元格上创建了​​两个单选按钮。这是一个问题的选项,但是当我选择它们时,它们都被选中了我不想要的,我只想选择其中一个,但我没有能够做到这一点......请帮助我这是我的 customCell 代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtnclick setTag:0];
    [leftBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [leftBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
    [leftBtnclick setFrame:CGRectMake(50, 120, 30, 30)];
    [leftBtnclick addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:leftBtnclick];

    rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtnclick setTag:1];
    [rightBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [rightBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
    [rightBtnclick setFrame:CGRectMake(180, 120, 30, 30)];
    [rightBtnclick addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:rightBtnclick];


    cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
    return cell;
}


-(void)leftTickBtnClicked:(id)sender
{
    if ([leftBtnclick isSelected]) {
        [sender setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];

    else
    {
      [sender setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

    }



}

-(void)rightTickBtnClicked:(id)sender
{
    if ([sender isSelected])
    {
         [sender setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    }



}

【问题讨论】:

    标签: ios objective-c ios7


    【解决方案1】:

    首先,设置leftBtnclick.tag=999rightBtnclick.tag=1000

    然后添加这段代码:-

    -(void)leftTickBtnClicked:(id)sender
    {
        UIButton *leftTickBtn=(UIButton *)sender;
        leftTickBtn.selected=!leftTickBtn.selected;
        for(UIView *vw in [[sender superview]subviews])
        {
            if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
            {
                UIButton *rightTickBtn=(UIButton *)vw;
    
                if(leftTickBtn.selected)
                {
                    rightTickBtn.selected=NO;
                }
                else
                {
                    rightTickBtn.selected=YES;
                }
    
            }
        }
    
    }
    -(void)rightTickBtnClicked:(id)sender
    {
    
        UIButton *rightTickBtn=(UIButton *)sender;
        rightTickBtn.selected=!rightTickBtn.selected;
    
    
        for(UIView *vw in [[sender superview]subviews])
        {
            if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
            {
                UIButton *leftTickBtn=(UIButton *)vw;
    
                if(rightTickBtn.selected)
                {
                    leftTickBtn.selected=NO;
                }
                else
                {
                    leftTickBtn.selected=YES;
                }
    
            }
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      在自定义表格单元格IBOutlet上添加四个按钮,并将这段代码写在cellforrowatindexpath中

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          static NSString *TableIdentifier = @"CELL";
          questionAireCell *cell = [questionAireTable dequeueReusableCellWithIdentifier:TableIdentifier];
      
          if (cell == nil)
          {
              NSArray *obj = [[NSBundle mainBundle] loadNibNamed:@"questionAireCell" owner:self options:nil];
              cell = [obj objectAtIndex:0];
          }
      
          cell.questionText.text=[cell.questionText.text stringByAppendingFormat:@"%ld",indexPath.row+1];
      
          cell.commentText.userInteractionEnabled=YES;
          cell.commentText.editable=YES;
          cell.commentText.tag = indexPath.row;
          cell.commentText.delegate = self;
      
          NSLog(@"Question Count=%ld",questionDescriptionArr.count);
      
          cell.questionText.text = [questionDescriptionArr objectAtIndex:indexPath.row];
          NSString *number = [[[NSString alloc]initWithString:[srNumberArr objectAtIndex:indexPath.row]]stringByAppendingString:@"."];
          cell.questionNoLbl.text = number;
      
      
          [cell.rightOptBtn addTarget:self action:@selector(rightOptBtnClicked:)  forControlEvents:UIControlEventTouchUpInside];
          [cell.rightTickBtn addTarget:self action:@selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
          [cell.leftOptBtn addTarget:self action:@selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
          [cell.leftTickBtn addTarget:self action:@selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
      
      //    [cell.rightOptBtn addTarget:self action:@selector(rightOptBtnClicked:)  forControlEvents:UIControlEventTouchUpInside];
      //    [cell.rightTickBtn addTarget:self action:@selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
      //    [cell.leftOptBtn addTarget:self action:@selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
      //    [cell.leftTickBtn addTarget:self action:@selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
      
          if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:@"YES"])
          {
              [cell.leftTickBtn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
              [cell.rightTickBtn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
          }
          else if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:@"NO"])
          {
              [cell.leftTickBtn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
              [cell.rightTickBtn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
      
          }
          else
          {
              [cell.leftTickBtn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
              [cell.rightTickBtn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
          }
      
      
          return cell;
      }
      
      
      -(void)leftOptBtnClicked:(UIButton *)sender
      {
      
          NSLog(@"Left btn clicked");
          CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
          NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
          [answerArray replaceObjectAtIndex:indexPath1.row withObject:@"YES"];
          [questionAireTable reloadData];
      
           NSLog(@"Answer Array: %@",answerArray);
      
      }
      
      -(void)rightOptBtnClicked:(UIButton *)sender
      {
          NSLog(@"Left btn clicked");
          CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
          NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
          [answerArray replaceObjectAtIndex:indexPath1.row withObject:@"NO"];
          [questionAireTable reloadData];
      
             NSLog(@"Answer Array: %@",answerArray);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-12
        • 2012-12-28
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        相关资源
        最近更新 更多