【发布时间】:2014-01-07 12:09:53
【问题描述】:
也许这是一个愚蠢的问题,但我觉得我可以改进我的项目,或者至少让它更具可读性。
这是交易。我有一个 UITableView 填充了来自服务器的数据。对于我使用MagicalRecord 和处理UITableView 的所有内容,我使用NSFetchedResultController。到目前为止,一切都很好。当我尝试实现UISearchDisplayController 时出现问题。不使用NSFetchedResultController,我只是从Core Data 中获取数据并将其传递给NSArray。这是我用来排序的方法:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
searchResults = [Group MR_findAllSortedBy:@"caseInsensitiveName" ascending:YES withPredicate:resultPredicate];
}
当然,所有UITableView 样板代码都填充了if 条件检查类型UITableView。
我想我可以使用一个NSFetchedResultController 来处理这两项任务(同时填充UITableView's)并省略这个searchResult 数组,但我不确定......所以我会很感激任何建议、提示或其他任何东西。
另一个问题是当我同时使用从其他来源填充的UITableViews 时,当我启用sections 时它们会发生冲突。简单地说,UISearchDisplayController.tableView 被父 UITableView 的 sections 覆盖。
编辑
这里是一些用于处理搜索和填充UITableView的代码
#pragma mark - Table View
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Count sections in FRC
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [[self.groupsFRC sections] count];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Count groups in each section of FRC
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[[self.groupsFRC sections] objectAtIndex:section] numberOfObjects];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get reusable cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
// If there isn't any create new one
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"];
}
Group *group = [self determineGroupInTableViev:tableView atIndexPath:indexPath];
cell.textLabel.text = group.name;
// Checking if group has been selected earlier
if ([self.selectedGroups containsObject:@{@"name" : group.name, @"id" : group.cashID}]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
// Adding checkmark to selected cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
Group *group = [self determineGroupInTableViev:tableView atIndexPath:indexPath];
// Checking if selected cell has accessory view set to checkmark and add group to selected groups array
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedGroups addObject:@{@"name" : group.name, @"id" : group.cashID}];
NSLog(@"%@", self.selectedGroups);
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedGroups removeObject:@{@"name" : group.name, @"id" : group.cashID}];
NSLog(@"%@", self.selectedGroups);
}
// Hiding selection with animation for nice and clean effect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
searchResults = [Group MR_findAllSortedBy:@"caseInsensitiveName" ascending:YES withPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
// Realoading main table view when search result did hide
[self.tableView reloadData];
}
【问题讨论】:
-
如果您使用的是魔法唱片,那么您为什么要使用 Fechcontroller?最好的方法是您使用一个通用数组并获取已过滤和未过滤的数据并重新加载您的表格视图
-
我使用 'NSFetchedResultController' 因为它提供了运行中的人口。
-
提供有关您的表视图和搜索控制器的详细信息。仅使用搜索栏并根据更改更新 FRC 谓词可能更容易......
-
@Wain 我已经用源代码更新了我的问题。如果您想要更多内容,请告诉我。
标签: ios uitableview nsfetchedresultscontroller uisearchdisplaycontroller magicalrecord