【问题标题】:SearchBar filtering and adapting the UITableView cells?SearchBar 过滤和调整 UITableView 单元格?
【发布时间】:2013-08-14 06:35:59
【问题描述】:

我的目标是使用搜索栏过滤 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];
}

【问题讨论】:

  • 为什么你在这里设置加号按钮的隐藏属性.. 并使用 reloadData 两次.. 让你明白。请多解释一些..
  • 这里,我试过如果过滤的内容是数据源,设置隐藏按钮的单元格保持不变

标签: ios uitableview uisearchbar


【解决方案1】:

从过滤列表中过滤单元格后,尝试在 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
}

【讨论】:

  • 我做到了,现在当我取消添加到右侧的过滤器单元格时应该带有隐藏按钮,但它们再次带有按钮。我需要保存单元格的状态和属性,但是如何保存?
  • 那个 NSARRAY 之一,您在其中枚举数组以设置隐藏属性。这个加号按钮是子视图还是 accesssoryView?
  • 这个加号按钮是自定义tableview单元格的子视图
  • 你有得到任何帮助吗??
猜你喜欢
  • 2018-04-09
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多