gpt4 book ai didi

ios - 一个用于 UITableview 和 UISearchDisplayController 的 NSFetchedResultController

转载 作者:行者123 更新时间:2023-11-29 03:21:55 25 4
gpt4 key购买 nike

也许这是一个愚蠢的问题,但我觉得我可以改进我的项目或至少使其更具可读性。

这是交易。我有一个 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 的)并忽略这个 searchResult 数组,但我不确定...因此,我将不胜感激任何建议、提示或其他内容。

还有一个问题是,当我使用从其他来源填充​​的两个 UITableViews 时,当我启用 sections 时,它们会发生冲突。简单地说,UISearchDisplayController.tableView 被父 UITableViewsection 覆盖。

编辑

这是一些用于处理搜索和填充 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];
}

最佳答案

我通常不觉得 UISearchDisplayController 很有用。这是因为:

  1. 我已经有了表格 View
  2. 表格 View 单元格的显示在搜索时应该是一样的,正常
  3. 我仍然需要提供所有的表委托(delegate)和数据源方法
  4. 我仍然需要提供搜索委托(delegate)方法

因此,我通常为 TableView 提供单一数据源,并根据输入的搜索条件操作该数据源。

这适用于 FRC 或简单数组。对于 FRC,编辑获取请求(谓词)和 performFetch:,其他一切都是自动的。对于数组,使用 sortedArrayUsingDescriptors: 并重新加载(我有源数据的原始数组,以及用于表方法的另一个数组引用。第二个数组指向原始数组实例或新的从排序创建的实例)。

在这两种情况下,我有类似数量的方法来使用 UISearchDisplayController,但在表方法中没有 if 语句。任何 if 语句和数据源的突变,仅在搜索方法中处理(调用频率要低得多)。


给类添加一个属性:

@property (strong, nonatomic) NSPredicate *filterPredicate;

在搜索文本中更改委托(delegate):

if (text.length > 0) {
self.filterPredicate = [NSPredicate predicateWithFormat:...];
} else {
self.filterPredicate = nil;
}

[self refreshFRC];

并且在搜索取消时,只是 nil 谓词并刷新。

刷新:

- (void)refreshFRC {
NSFetchRequest *fetch = ...;

if (self.filterPredicate != nil) {
fetch.predicate = self.filterPredicate;
}

self.frc.fetchRequest = fetch;

[self.frc performFetch:...];
}

关于ios - 一个用于 UITableview 和 UISearchDisplayController 的 NSFetchedResultController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com