滚动UITableView会重置单元格的复选框按钮状态

时间:2014-01-23 12:03:29

标签: ios uitableview uibutton

我知道这里有很多类似的问题,但没有一个能解决我的问题。我的UITableView中有很多部分,当我单击/选中单元格中的某些按钮并向下滚动时,复选标记会在再次向上滚动时消失。这是我编码的内容..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"CheckBoxedCell";
   // NSString *cellId = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row];
    CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];

    if(!cell)
    {
        NSArray *nib;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass" owner:self options:nil];
        }
        else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass_iPad" owner:self options:nil];
        }
            for (id object in nib)
            {
                if([object isKindOfClass:[CheckBoxedCellClass class]])
                {
                    cell = (CheckBoxedCellClass *)object;
                    break;
                }
            }

            cell = [nib objectAtIndex:0];

     }

        SaveCheckBoxedView *saveContact;
        if(isFiltered == YES)
        {
            saveContact = [filterdArray objectAtIndex:indexPath.row];
            cell.nameLabel.text = saveContact.nameString;
        }
        else
        {
            saveContact = [mutableArray objectAtIndex:indexPath.row];
            cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        }


        cell.companyLabel.text = saveContact.companyString;
        cell.invIdLabel.text = [NSString stringWithFormat:@"%@", saveContact.invitId];

        //set fonts
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {

            [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]];
        }
        else
        {

            [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]];
        }



    //handling check box

    NSInteger rowNumber = 0;
    for(NSInteger i = 0; i < indexPath.section ; i++)
    {
        rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
    }

    rowNumber += indexPath.row;


    UIButton *checkBox;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
    }
    else
    {
        checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
    }

    [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
    [checkBox addTarget:self action:@selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];

    checkBox.tag = rowNumber;

    [cell.contentView addSubview:checkBox];

    // handle check box view reset when scrolled

    for(NSIndexPath *path in checkedIndexPaths)
    {
        if(path.section == indexPath.section && path.row == indexPath.row)
        {
            if(!checkedIndexPaths)
            {
                checkedIndexPaths = [[NSMutableSet alloc] init];
                [checkedIndexPaths addObject:[NSNumber numberWithInt:rowNumber]];
            }
        }
    }

    return cell;
}


-(void)checkBoxClicked:(id)sender event:(id)event
{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableViewContact];
    NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"value of indexPath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);

    UIButton *tappedButton = (UIButton*)sender;
    NSLog(@"Tag number = %d", [sender tag]);


    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]])
    {
        [sender  setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal];
        if(isFiltered == YES)
        {
            NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row];
            NSLog(@"filterd id = %@", addId); //get filtered array here
            [arrayOfIds addObject:addId];
        }
        else
        {
            NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
            NSLog(@"Tagged checked button id = %@", finalIntId);
            [arrayOfIds addObject:finalIntId];
        }

    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal];
        NSLog(@"UnChecked");

        [arrayOfIds removeObjectAtIndex:tappedButton.tag];
    }

}

3 个答案:

答案 0 :(得分:0)

如果我没弄错,你永远不会设置checkBox.checked?

答案 1 :(得分:0)

当您向上和向下滚动时,只有可见单元格会出列并配置。因此,每当您向下滚动整个页面然后再向上时,将重新创建单元格(您可以在NSLog的入口点cellForRowAtIndexPath查看我的意思。

在此方法中,您可以重新创建按钮。请记住,表格视图单元格是缓存所以如果您创建了一个单元格,它可能会在需要时再次使用,因此只有在第一次创建单元格时才创建按钮,而不是每次调用{ {1}}。

如果您使用自定义单元格,则可以添加cellForRowAtIndexPath成员以指示该单元格已经创建,并且无需添加另一个新创建的按钮,当您使用{添加它时隐藏原始单元格{1}}(或者更好的是在这个自定义单元格的init方法中创建按钮,这将更容易和更正确)

答案 2 :(得分:0)

我曾经用按钮实现类似的东西。因此,在单元格内部,您可以定义一个更改复选框背景的功能,或者根据模型更改复选框的状态。

-(void) setModel:(Model *)model {

    [self.button setSomeStyleBasedOnYourModel];        
}

如果您的故事板的tableview中有该复选框,则可以将故事板中的目标链接到您的控制器。

- (IBAction)doTaskButtonPressed:(UIButton *)sender {
    [super doTaskButtonPressed:sender];
}