滚动UITableView时,单选按钮被取消选中

时间:2015-08-05 07:30:19

标签: ios iphone ios7

我在UITableView Cell上有两个单选按钮。我可以在向下滚动UITableView时选择其中一个,但是当我滚动tableview时,所有单选按钮都会被取消选中。滚动后我也想让他们选中但我无法做到这一点......所以请有任何解决方案帮助我。谢谢

- (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.tag=999;
    [leftBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [leftBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

    if (screenHeight == 667)
    {
        [leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
     }
    else if(screenHeight == 480)
    {
        [leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];

    }

    [leftBtnclick addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.leftOptBtn addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:leftBtnclick];

    rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtnclick.tag=1000;
    [rightBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [rightBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

    if (screenHeight == 667)
    {
       [rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
    }
    else if(screenHeight == 480)
    {
        [leftBtnclick setFrame:CGRectMake(50, 40, 30, 30)];

    }

    [rightBtnclick setFrame:CGRectMake(180, 59, 30, 30)];
    [rightBtnclick addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.rightOptBtn addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:rightBtnclick];

    cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
    cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
    [cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    return cell;
}

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

        }
    }


}

3 个答案:

答案 0 :(得分:0)

您只创建了一个leftTickBtnrightTickBtn,并在每次创建行时更新它。所以无论你用单选按钮做了什么,它只会影响到最后一个按钮。

更新您的代码,如下所示:

UIButton *leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtnclick.tag=999+indexPath.row;

UIButton *rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtnclick.tag=1000+indexPath.row;

并实施以下方法:

- (IBAction)leftTickBtnClicked:(UIButton *)sender
{
    sender.selected = TRUE;
    UITableViewCell *cell = [tblAppointment cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1000-sender.tag inSection:0]];

    for (id view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            // your code
             UIButton *btnRight = (UIButton *)view;
             if (sender.selected == TRUE)
                 btnRight.selected = FALSE;
             else
                 btnRight.selected = TRUE;
        }
    }
}

- (IBAction)rightTickBtnClicked:(UIButton *)sender
{
    sender.selected = TRUE;
    UITableViewCell *cell = [tblAppointment cellForRowAtIndexPath:[NSIndexPath indexPathForRow:999-sender.tag inSection:0]];

    for (id view in cell.contentView.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            // your code
             UIButton *btnLeft = (UIButton *)view;
             if (sender.selected == TRUE)
                 btnLeft.selected = FALSE;
             else
                 btnLeft.selected = TRUE;
        }
    }
}

答案 1 :(得分:0)

由于可重用单元的工作原理,您遇到此问题。 当您从单元格滚动离开时,您在单选按钮上进行了更改,然后尝试向后滚动到该特定行,可重复使用的单元格dequeueReusableCellWithIdentifier不保证它将使您更改的完全相同的单元格出列并且最有可能的是它会使您没有进行任何更改的单元格出列

您需要跟踪cellForRowAtIndexPath之外的列表中的按钮选项,并将选择重新分配到cellForRowAtIndexPath中的单选按钮,就像每次使用{分配单元格文本时的相同想法一样{1}}通过在数组中使用indexPath.row

请参阅以下代码中的修改[1],[2]和[3](尚未对其进行测试,但您明白了这一点)

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

答案 2 :(得分:0)

我的问题得到了解决方案。当滚动tableview单元格被重用时,UITableView单元格是可重用的,这就是单选按钮选择消失的原因。所以解决方案是将你的选择存储在一个数组中,使用cellForRowAtIndexPath中的数组。见代码。

- (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.tag=999;
    cell.leftOptBtn.tag=999;
    [leftBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [leftBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
    [leftBtnclick setFrame:CGRectMake(50, 59, 30, 30)];
    [leftBtnclick addTarget:self action:@selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.leftOptBtn addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:leftBtnclick];


    rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtnclick.tag=1000;
    cell.rightTickBtn.tag=1000;
    [rightBtnclick setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [rightBtnclick setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
    [rightBtnclick setFrame:CGRectMake(190, 59, 30, 30)];


    [rightBtnclick addTarget:self action:@selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.rightOptBtn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:rightBtnclick];

    cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
    cell.questionLbl.text = [questionArray objectAtIndex:indexPath.row];
    [cell.leftOptBtn setTitle:[leftOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [cell.rightOptBtn setTitle:[rightOptionArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

    if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:@"YES"])
    {
        leftBtnclick.selected=YES;
        rightBtnclick.selected=NO;

    }
    else if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:@"NO"])
    {
        leftBtnclick.selected=NO;
        rightBtnclick.selected=YES;
    }
    else
    {
        leftBtnclick.selected=NO;
        rightBtnclick.selected=NO;
    }
    return cell;
}



-(void)leftTickBtnClicked:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionTable];
    NSIndexPath *indexPath1 = [questionTable indexPathForRowAtPoint:buttonPosition];
    [answerArray replaceObjectAtIndex:indexPath1.row withObject:@"YES"];

    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;

            }


        }
    }
    NSLog(@"Answer Array: %@",answerArray);
}

-(void)rightTickBtnClicked:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionTable];
    NSIndexPath *indexPath1 = [questionTable indexPathForRowAtPoint:buttonPosition];
    [answerArray replaceObjectAtIndex:indexPath1.row withObject:@"NO"];

    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;

            }

        }
    }
    NSLog(@"Answer Array: %@",answerArray);

}