0

我的目标是使用搜索栏过滤 tableview 的单元格。关键是我在自定义单元格中有一个按钮,这个按钮是否隐藏它被添加到右侧的列表中(这是另一个uiview)

例如,我使用右侧的加号按钮和隐藏加号按钮添加了一个单元格,并使用搜索栏进行过滤。当我过滤我添加的单元格时,它应该带有隐藏的加号按钮。因为它已经在过滤之前添加了。

我已经为 searchBar 的 textDidChange 方法编写了一些代码,但仍然无法做到。

如何实现这个目标?

我的试用代码;

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{


        NSArray *cells = [tableView visibleCells];

    for (DoctorListCell *cell in cells)
    {
        NSLog(@"Cell: %d",cell.plusButton.tag);

        if(cell.plusButton.tag==0)
        {
                            [cell.plusButton setHidden:YES];

                            [tableView reloadData];
        }

    }

if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    filteredListContent = [[NSMutableArray alloc] init];

    for (PlannedCustomer* docs in doctorsTable)
    {
        NSRange nameRange = [docs.customerName rangeOfString:text options:NSCaseInsensitiveSearch];
        if(nameRange.location != NSNotFound)
        {
            [filteredListContent addObject:docs];
        }
    }
}

[self.tableView reloadData];
}
4

1 回答 1

0

cellForRowAtIndexpath从过滤列表中过滤单元格后,只需尝试在您的方法中设置单元格的隐藏属性。或者你可以设置隐藏在tableView:willDisplayCell:forRowAtIndexPath:

更新:

如果您的加号按钮是子视图,则覆盖然后在您的自定义单元格类中的某个方法中设置隐藏属性。然后调用那个方法。

或者您可以删除加号按钮而不是隐藏。

例如:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *nib;
    static NSString *cellIdentifier= @"cell";

    UITableViewCell *theCell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];

    if([theCell.contentView subviews]){
        for(UIView *view in [theCell.contentView subviews]){
            [view removeFromSuperview];
        }
    }

    if(theCell== nil)
    {
        nib  = [[NSBundle mainBundle] loadNibNamed:@"Your custom cell name" owner:self options:nil]; 
        theCell = [nib objectAtIndex:0];
        theCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    UIButton *btn=(UIButton*)[theCell.contentView viewWithTag:101];
if(yourcondition)
//hide button
else
//show button
}
于 2013-08-14T06:59:56.937 回答