【问题标题】:UISearchBar with table view and checkbox tutorial带有表格视图和复选框的 UISearchBar 教程
【发布时间】:2014-02-11 10:32:16
【问题描述】:

谁能推荐一个教程,它可以为一个 ViewController 完成以下 3 个任务。

1. Search names in tableView.
2. Each cell in tableView has checkBoxes.
3. Retain checked cells [state of `UIButtons`] if the user searches the name.

到目前为止,我已经完成了 lengthly code 的前两个任务 但无法达到第三点。任何帮助将不胜感激。谢谢。

【问题讨论】:

标签: ios objective-c uitableview uibutton uisearchbar


【解决方案1】:

有很多方法可以做到这一点。你不能指望教程完全按照你正在做的事情。

有很多方法可以做到这一点。其中一种方法是将选中单元格的状态存储在NSArray 中,然后在cellForRowAtIndexPath 数据源方法中加载状态。

【讨论】:

【解决方案2】:

用下面的代码解决了我自己的问题。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *cellId = @"CheckBoxedCell";
    NSString *cellId = [NSString stringWithFormat:@"S%1dR%1d",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];

    }

    //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;

    SaveCheckBoxedView *saveContact;
    if(isFiltered == YES)
    {
        saveContact = [filterdArray objectAtIndex:indexPath.row];
        cell.nameLabel.text = saveContact.nameString;
        cell.companyLabel.text = saveContact.companyString;
    }
    else
    {
        saveContact = [mutableArray objectAtIndex:indexPath.row];
        cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        cell.companyLabel.text = [NSString stringWithFormat:@"%@", [companyArray objectAtIndex:rowNumber]];
    }

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

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


    // handle check box view reset when scrolled



    if(isFiltered == YES)
    {
        NSLog(@"filtered");
        BOOL buttonPress = [[boolDictForSearch objectForKey:[filteredArrayOfIds objectAtIndex:indexPath.row]] boolValue];
        NSLog(@"button press = %d", buttonPress);
        [checkBox setSelected:buttonPress];
        if(buttonPress)
        {
            [checkBox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        }
        else
        {
            [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];

        }
        checkBox.tag = indexPath.row;
    }
    else
    {
        BOOL buttonPressed = [[boolDict objectForKey:[NSString stringWithFormat:@"%d", rowNumber]] boolValue];
        NSLog(@"button pressED = %d", buttonPressed);
        NSLog(@"Row number = %d", rowNumber);
        [checkBox setSelected:buttonPressed];


        if(buttonPressed)
        {
            [checkBox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        }
        else
        {
            [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];

        }
        checkBox.tag = rowNumber;
    }
    [cell.contentView addSubview:checkBox];

    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];
        NSUserDefaults *buttonDefault = [NSUserDefaults standardUserDefaults];
        [buttonDefault setBool:YES forKey:@"CHECKMARKEDKEY"];


        [self.boolDict setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];
        //[self.boolDictForSearch setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%@", saveContact.nameString]];

        if(isFiltered == YES)
        {
            NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row];

            [self.boolDictForSearch setObject:[NSNumber numberWithBool:YES] forKey:addId];

            NSLog(@"filterd id = %@", addId); //get filtered array here
            [arrayOfIds addObject:addId];
        }
        else
        {
            NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];

            [self.boolDictForSearch setObject:[NSNumber numberWithBool:YES] forKey:finalIntId];

            NSLog(@"Tagged checked button id = %@", finalIntId);
            [arrayOfIds addObject:finalIntId];
        }

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

        [self.boolDict setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];
        //[self.boolDictForSearch setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%@", saveContact.nameString]];

        if(isFiltered == YES)
        {
            [arrayOfIds removeObjectIdenticalTo:[filteredArrayOfIds objectAtIndex:tappedButton.tag]];
            [self.boolDictForSearch setObject:[NSNumber numberWithBool:NO] forKey:[filteredArrayOfIds objectAtIndex:tappedButton.tag]];
        }
        else
        {
            [arrayOfIds removeObjectIdenticalTo:[mutableArrayOfIds objectAtIndex:tappedButton.tag]];
            [self.boolDictForSearch setObject:[NSNumber numberWithBool:NO] forKey:[mutableArrayOfIds objectAtIndex:tappedButton.tag]];
        }
    }
}

【讨论】:

  • ಠ_ಠ 这正是我的解决方案。
  • 伙计们,我已遵循本指南并且一切正常,我唯一的问题......当我在正常模式下选择检查时,当我进入搜索模式(使用 UISearchBar)时,之前的检查不是更多'显示为未经检查的返回......我在哪里做错了?
猜你喜欢
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多