UISearchBar的按钮标记问题

时间:2014-01-21 09:38:40

标签: ios uitableview uisearchbar uisearchbardisplaycontrol

我需要为表格视图单元格上创建的按钮设置标签 我实现了UISearchBar,效果很好。

问题是在过滤 tableView单元格后,其indexpath.row在我设置时发生了变化 'button.tag = indexPath.row'

有没有办法让每个单元格的行号保持不变? 或者还有其他解决方案吗?

注意:我在一个UITableView中有多个部分,isFiltered是BOOL值,表示用户已开始在UISearchBar中键入文字。

https://github.com/kwylez/IndexedTable

的帮助下实施搜索

cellForRowAtIndexPath:

     - (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.nameLabel.text = saveContact.nameString;

        cell.companyLabel.text = saveContact.companyString;
        cell.invIdLabel.text = [NSString stringWithFormat:@"%d", 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;

    /*if([indexPath compare:self.lastIndexPath] == NSOrderedSame)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        NSString *finalIntId = [mutableArrayOfIds objectAtIndex:rowNumber];
        NSLog(@"Tagged checked button id = %@", finalIntId);
        [arrayOfIds addObject:finalIntId];
    }*/

    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];
    if(isFiltered == YES)
    {
        checkBox.tag = ;
    }
    else
    {
        checkBox.tag = rowNumber;
    }
    [cell.contentView addSubview:checkBox];

    return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if(isFiltered == YES) {
        return Nil;
    } else {
        NSArray *toBeReturned = [NSArray arrayWithArray:
                                 [@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
                                  componentsSeparatedByString:@"|"]];
        return toBeReturned;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (title == UITableViewIndexSearch) {
        CGRect searchBarFrame = self.searchDisplayController.searchBar.frame;
        [tableView scrollRectToVisible:searchBarFrame animated:YES];

        return -1;
    } else {
        NSInteger count = 0;

        for (NSString *character in arrayOfCharacters) {
            if ([character isEqualToString:title]) {
                return count;
            }
            count ++;
        }
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(isFiltered == YES) {
        return 1;
    } else {
        return [arrayOfCharacters count];
        //return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(isFiltered == YES) {
        return [filterdArray count];
    } else {
        //return [mutableArray count];
        return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([arrayOfCharacters count] == 0) {
        return @"";
    }

    return [NSString stringWithFormat:@"%@", [arrayOfCharacters objectAtIndex:section]];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if(searchText.length == 0)
    {
        isFiltered = NO;
    }
    else
    {
        isFiltered = YES;
        filterdArray = [[NSMutableArray alloc] init];
        for (SaveCheckBoxedView *contact in mutableArray)
        {
            NSRange nameRange = [contact.nameString rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(nameRange.location != NSNotFound)
            {
                [filterdArray addObject:contact];
            }
        }
    }
    [self.tableViewContact reloadData];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [filterdArray removeAllObjects];
    if(searchString.length > 0)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.mySearchBar.text];
        for (NSString *key in arrayOfCharacters)
        {
            NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
            [filterdArray addObjectsFromArray:matches];
        }
    }
    return YES;
}

1 个答案:

答案 0 :(得分:0)

我正在描述一种方法,你可能会找到更好的方法。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [filterdArray removeAllObjects];
    if(searchString.length > 0)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.mySearchBar.text];
        for (NSString *key in arrayOfCharacters)
        {
            NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
            /****see bellow****/
            [filterdArray addObjectsFromArray:matches];
        }
    }
    return YES;
}

尝试查找“匹配”的每个对象的行号。这将是你的按钮的标签。使filterdArray成为一个字典数组。在字典中添加2个字段。一个用于标记另一个用于值。在 cellForRowAtIndexPath

 if(isFiltered == YES)
{
    checkBox.tag = [filterdArray objectForKey: @"tag"] ;
}
else
{
    checkBox.tag = rowNumber;
}