如何在uitableview单元格上创建两个单选按钮

时间:2015-08-04 13:23:32

标签: ios objective-c ios7

我在一个tableview单元格上创建了​​两个单选按钮。这是一个问题的选项,但是当我选择它们时,它们都被选中,我不想要,我只想选择其中一个,但我不是能够做到这一点......请帮帮我这是我的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];
    }



}

2 个答案:

答案 0 :(得分: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;
            }

        }
    }


}

答案 1 :(得分:0)

在自定义表格单元格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);

}