如何使用filteredArrayUsingPredicate来过滤UITableView?

时间:2013-12-02 09:37:53

标签: uitableview grouping

感谢您提前帮忙。

尝试按照此解决方案按字母顺序对UITableView进行自动分组:yasirmturk solution但是我被困在这个部分,他说:

  

按字母过滤后:

NSArray *sectionArray = [vendorList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.sections objectAtIndex:section]]]; 
        rowCount = [sectionArray count];

问题:我应该在哪里放置NSArray * sectionArray代码?更具体地说?

当前状态Current Status (repeated array without filtering)

预期结果

1)“蛋本尼迪克特”应在“E”部分下,“火腿和蛋三明治”在“H”部分下。

1 个答案:

答案 0 :(得分:1)

中添加它
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.isFiltered){
        NSArray *filteredSectionArray = [filteredTableData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [arrSectionsHeader objectAtIndex:section]]];
        return [filteredSectionArray count];
    }
    else{
        NSArray *sectionArray = [allTableData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [arrSectionsHeader objectAtIndex:section]]];
        return [sectionArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//....
    if (isFiltered) {
        NSArray *filteredSectionArray = [filteredTableData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [arrSectionsHeader objectAtIndex:indexPath.section]]];
        cell.nameLabel.text =[filteredSectionArray objectAtIndex:indexPath.row];
    }else{
        NSArray *sectionArray = [allTableData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [arrSectionsHeader objectAtIndex:indexPath.section]]];
        cell.nameLabel.text = [sectionArray objectAtIndex:indexPath.row];
    }
//....
}

=)