gpt4 book ai didi

ios - NSFetchedResultsController 在排序时检索特定数据

转载 作者:行者123 更新时间:2023-11-29 01:27:45 25 4
gpt4 key购买 nike

我创建了一个 NSFetchedResultsController对象并用来自核心数据实体(称为“AssetsPartners”)的数据加载它。这个实体有很多不同的属性,比如name , type , photo等等。目前,我使用 name数据获取期间的属性,这会带回所有内容。现在我需要能够对 NSFetchedResultsController 中的内容进行排序当按下 3 个之一时 UISegmentedControl “类型”属性的按钮。 type 属性仅包含 3 个可能的值:all、asset & counterparty。索引 0 =“全部”,索引 1 =“ Assets ”,索引 2 =“交易对手”。因此,当用户单击索引 1 时,例如“Assets”,我只想从我的 NSFetchedResultsController 中提取该类型的数据。在 TableView 中显示该数据之前。我该怎么做?

下面是我如何创建 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;
}

获得数据后,我将其放入 TableView 中:

-(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;
}

最佳答案

您可以更新与 NSFetchResultController 关联的 fetchRequest NSPredicate。这是示例代码。

-(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

关于ios - NSFetchedResultsController 在排序时检索特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825499/

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