我创建了一个NSFetchedResultsController
对象&用Core Data实体(称为“AssetsPartners”)的数据加载它。此实体具有许多不同的属性,例如name
,type
,photo
等。目前,我在数据提取期间使用name
属性,这会带回所有内容。现在,当按'type'属性按下3个NSFetchedResultsController
按钮之一时,我需要能够对UISegmentedControl
内的内容进行排序。 type属性仅包含3个可能的值:all,asset&的对手。索引0 =“全部”,索引1 =“资产”,索引2 =“交易对手”。因此,当用户点击索引1时,例如“Assets
”,我想在表格视图中显示该数据之前,仅从我的NSFetchedResultsController
中提取该类型的数据。我该怎么做?
以下是我创建NSFetchedResultsController
:
-(NSFetchedResultsController *)fetchedResultsController
{
//Is there already anything in that instance variable? Not equalling to nil means there's something alreayd there. Just return it & we're done.
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
//IF we make it past the previous if statement, we'll have to make the previous object.
//We need a NSFetchRequest & a sort descriptor to create a NSFetchedResultsContoller.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AssetsPartners" inManagedObjectContext:[AppDelegate shared].managedObjectContext];
[fetchRequest setEntity:entity];
//Specify how the fetched objects should be sorted.
NSSortDescriptor *assetName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; //type //Sorts data by name. In this case by "name"'s first letter of the alphabet.
[fetchRequest setSortDescriptors:@[assetName]]; //@[fetched, assetType, dateSD];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[AppDelegate shared].managedObjectContext
sectionNameKeyPath:nil //'nil' brings data back in one long list. But writing "author", for instance, would split data by work written by each different author.
cacheName:nil]; //Not sure what this does...
_fetchedResultsController.delegate = self; //"Look to me for any of your delegate methods".
return _fetchedResultsController;
}
获得数据后,我将其放入表格视图中:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Has to be here to be accessible by any object that requires it regarding of scope.
AssetsPartners *items;
//Segmented control stuff. Simply filter fetched array data rather than creating everything cell-related from scratch.
if(_segmentedControl.selectedSegmentIndex == 0)
{
items = [_fetchedResultsController objectAtIndexPath:indexPath]; //Ask '_fetchedResultsController' what's at this particular indexPath right now. Also alphabetically sorts the array list.
}
else if(_segmentedControl.selectedSegmentIndex == 1)
{
//Sorts table view alphabetically.
//[self.elements sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];
}
else
{
}
CustomTableCell *aCell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; //Cells are dequeued & reused.
if (aCell == nil)
{
aCell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CustomCell"];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
label.textColor = [UIColor whiteColor];
label.tag = 999; //Neccessary to have this to by-pass scope & access this label outside this if statement (Chosen number is random).
[aCell.contentView addSubview:label];
//Entirely new UIImageView for every UITableViewCell (to tweak the frame size & position).
UIImageView *newImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 15.0, 35.0, 35.0)];
newImgView.tag = 1000;
[aCell.contentView addSubview:newImgView];
}
//Makes sure the separator doesn't have that little gap in the beginning.
//NOTE: This MUST be here and "_tableView.separatorInset = UIEdgeInsetsZero;" must exist inside [self extraTableViewParameter]!
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
aCell.layoutMargins = UIEdgeInsetsZero;
aCell.preservesSuperviewLayoutMargins = NO;
}
/*CELL DETAILS.*/
aCell.backgroundColor = [UIColor clearColor];
aCell.textLabel.textColor = [UIColor clearColor];
aCell.detailTextLabel.textColor = [UIColor lightGrayColor];
/*FILL CELL WITH DATA.*/
aCell.textLabel.text = @"";
NSInteger anInteger = [items.balance integerValue];
aCell.detailTextLabel.text = [NSString stringWithFormat:@"%@ P", [self addWhitespacesTo:anInteger]];
UILabel *label = (UILabel *)[aCell.contentView viewWithTag:999]; //Casting UILabel to access the previously created one.
label.text = items.name;
//1 - Reduce 'photo' characters to bare essentials. Which is just an image name.
NSString *iconName = [self cutOutString:@"bizzer://" fromString:items.photo]; //Get rid of "bizzer://" from string value.
//2 - Utilize previously created UIImageView for this cell with the image name.
UIImageView *imgView = (UIImageView *)[aCell.contentView viewWithTag:1000];
imgView.image = [UIImage imageNamed:iconName];
[self turnIcon:imgView toColor:items.color];
return aCell;
}
答案 0 :(得分:1)
您可以更新与NSPredicate
相关联的fetchRequest NSFetchResultController
。这是示例代码。
-(void)updateFetchRequestForType:(NSString *)type
{
[NSFetchedResultsController deleteCacheWithName:@"Root"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@",type];
[_fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[table reloadData];
}
注意:在您的代码中初始化NSFetchResultController时,将cacheName设置为 Root 。
答案 1 :(得分:0)
(这不是一个很好的解决方案,但我把它放在这里让其他人知道为什么并让他们免受挫折)。
我这样解决了。我非常清楚这不是最优雅的解决方案,但它对我有用。虽然它可能不太理想,但我试图提供帮助,而且我非常愿意学习更好的技术。
为您的NSPredicate创建一个属性,并为其提供一个默认值(例如" All"或任何适合您的内容)。
为您的分段控制创建一个操作,在值更改时执行以下操作: