iOS-按UISegmentedControl筛选UITableView
我有一个带有搜索栏的iOS-按UISegmentedControl筛选UITableView,ios,uitableview,filter,uisegmentedcontrol,Ios,Uitableview,Filter,Uisegmentedcontrol,我有一个带有搜索栏的UITableViewController(在UINavigationController中实现)。我在导航栏中添加了一个UISegmentedControl,以便在表视图中实现一个过滤器。我的表格视图显示一个NSArray,其中包含联系人列表(自定义contact对象)。我想按联系人上的属性之一进行筛选(我们称之为属性)。我已经有2个数组来处理搜索车过滤。要按属性设置新筛选器,是否必须创建一个新数组,该数组仅包含具有属性的联系人?或者有没有一种聪明的方法可以在不创建其他阵列
UITableViewController
(在UINavigationController
中实现)。我在导航栏中添加了一个UISegmentedControl
,以便在表视图中实现一个过滤器。我的表格视图显示一个NSArray
,其中包含联系人列表(自定义contact
对象)。我想按联系人
上的属性之一进行筛选(我们称之为属性
)。我已经有2个数组来处理搜索车过滤。要按属性设置新筛选器,是否必须创建一个新数组,该数组仅包含具有属性的联系人?或者有没有一种聪明的方法可以在不创建其他阵列的情况下对其进行过滤
我希望避免创建另一个数组的原因是,我已经有2个用于搜索筛选的数组。如果我要为UISegmentedControl
过滤器创建另一个数组,我想我必须创建另外两个数组-一个用于属性,不带搜索过滤器,另一个用于属性,带搜索过滤器。这取决于您保存数据的方式,但是,如果您使用的是核心数据和NSFetchedResultsController,那么下面类似的方法就可以工作了。我提供了两个分段控制器视图,允许用户筛选日期范围、供应商等,然后构建一个复合谓词并刷新表数据
- (void)setupFetchedResultsController
{
// 1 - Set the Entity
NSString *entityName = @"Invoice";
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter if needed
// Use a NSCompoundPredicate
// This is a complex predicate as user can choose :
// Payment Status
// Invoice Date range
// Supplier
// Constant - always filter by AP
NSPredicate *predicateAP;
predicateAP = [NSPredicate predicateWithFormat:@"accountingType = %@ ",@"AP"];
// Array of predicates
NSMutableArray *predicateMutArray = [[NSMutableArray alloc]initWithObjects:predicateAP, nil];
// Invoice payment Status
NSPredicate *predicatePaymentStatus;
if (self.filterSelectString){
if ([self.filterSelectString isEqualToString:@"Paid"]){
predicatePaymentStatus = [NSPredicate predicateWithFormat:@"paymentReference = %@ AND accountingType = %@ ",@"PAID",@"AP"];
}
if ([self.filterSelectString isEqualToString:@"Unpaid"]){
predicatePaymentStatus = [NSPredicate predicateWithFormat:@"paymentReference != %@ AND accountingType = %@ ",@"PAID",@"AP"];
}
}
if (predicatePaymentStatus){
[predicateMutArray insertObject:predicatePaymentStatus atIndex:[predicateMutArray count]];
}
// Invoice Date Range
NSPredicate *predicateDateRange;
if ([self fromDate] && [self toDate]){
predicateDateRange = [NSPredicate predicateWithFormat:@"dateInvoiceIssued >= %@ AND dateInvoiceIssued <= %@",[self fromDate], [self toDate]];
}
if (predicateDateRange){
[predicateMutArray insertObject:predicateDateRange atIndex:[predicateMutArray count]];
}
// Invoice Supplier
NSPredicate *predicateSupplierSelected;
if (self.selectedSupplier){
predicateSupplierSelected = [NSPredicate predicateWithFormat:@"supplier.supplierName = %@ ",[self.selectedSupplier supplierName]];
}
if (predicateSupplierSelected){
[predicateMutArray insertObject:predicateSupplierSelected atIndex:[predicateMutArray count]];
}
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateMutArray];
// 4 - Sort it
// Array of sortDescriptors
NSSortDescriptor *sortOnSupplier = [NSSortDescriptor sortDescriptorWithKey:@"supplier.supplierName"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *sortOnDateIssued = [NSSortDescriptor sortDescriptorWithKey:@"dateInvoiceIssued"
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableArray *sortDescriptorArray = [[NSMutableArray alloc]initWithObjects:sortOnSupplier, nil];
[sortDescriptorArray insertObject:sortOnDateIssued atIndex:[sortDescriptorArray count]];
request.sortDescriptors = sortDescriptorArray;
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"supplierNameSectionTitle"
cacheName:nil];
[self performFetch];
-(无效)setupFetchedResultsController
{
//1-设置实体
NSString*entityName=@“发票”;
//2-请求该实体
NSFetchRequest*request=[NSFetchRequest fetchRequestWithEntityName:entityName];
//3-必要时进行过滤
//使用复合谓词
//这是一个复杂的谓词,用户可以选择:
//支付状态
//发票日期范围
//供应商
//常数-始终按AP过滤
NSPredicate*predicateAP;
predicateAP=[NSPredicate predicateWithFormat:@“accountingType=%@”,“AP”];
//谓词数组
NSMutableArray*谓词数组=[[NSMutableArray alloc]initWithObjects:predicateAP,nil];
//发票付款状态
NSPredicate*谓词支付状态;
if(self.filterSelectString){
如果([self.filterSelectString IsequalString:@“Paid”]){
predicatePaymentStatus=[NSPredicate predicateWithFormat:@“paymentReference=%@和accountingType=%@“,@“PAID”,@“AP”];
}
如果([self.filterSelectString IsequalString:@“未付”]){
predicatePaymentStatus=[NSPredicate predicateWithFormat:@“paymentReference!=%@和accountingType=%@“,@“PAID”,@“AP”];
}
}
if(谓词支付状态){
[predicateMutArray insertObject:predicatePaymentStatus atIndex:[predicateMutArray计数];
}
//发票日期范围
NSPredicate*预测数据集;
如果([self fromDate]&&[self toDate]){
PredicatedTerange=[NSPredicate predicateWithFormat:@“DateInvoiceIssuited>=%@和DateInvoiceIssuited