用于根据相关数据进行筛选的iOS核心数据谓词
好吧,我是个傻瓜。他们对我来说是陌生的 关于应用程序: 我有一个处理游戏比赛的应用程序。有球员、签到和比赛的实体。 这样做的目的是将玩家添加到应用程序中,然后可以登录进行比赛,并存储比赛结果 关系: 玩家>签入(每个玩家可以在不同日期有多个签入)用于根据相关数据进行筛选的iOS核心数据谓词,ios,entity-framework,core-data,nspredicate,nsfetchrequest,Ios,Entity Framework,Core Data,Nspredicate,Nsfetchrequest,好吧,我是个傻瓜。他们对我来说是陌生的 关于应用程序: 我有一个处理游戏比赛的应用程序。有球员、签到和比赛的实体。 这样做的目的是将玩家添加到应用程序中,然后可以登录进行比赛,并存储比赛结果 关系: 玩家>签入(每个玩家可以在不同日期有多个签入) 发件人:玩家实体 关系:playerCheckins 反向:棋盘手 目的地:签入实体 球员比赛(每场比赛可以有两名球员,球员在每次比赛中可以有多场比赛) 发件人:玩家实体 关系:playerMatches 反向:棋手 目标:匹配实体 我有一个共
- 发件人:玩家实体
- 关系:playerCheckins
- 反向:棋盘手
- 目的地:签入实体
- 发件人:玩家实体
- 关系:playerMatches
- 反向:棋手
- 目标:匹配实体
PlayersCollectionViewController.h
#import "CoreCollectionViewController.h"
#import "Player.h"
@protocol PlayersCollectionViewControllerDelegate;
@interface PlayersCollectionViewController : CoreCollectionViewController <NSFetchedResultsControllerDelegate>
@property (nonatomic, assign) id <PlayersCollectionViewControllerDelegate> delegate;
@property (nonatomic, strong) NSString *titleText;
@property (nonatomic, strong) NSString *predicate;
- (IBAction)cancel:(UIBarButtonItem *)sender;
- (IBAction)done:(UIBarButtonItem *)sender;
@end
@protocol PlayersCollectionViewControllerDelegate <NSObject>
@optional
-(void)ViewController:(UIViewController *)sender
didSelectPlayer:(Player *)selectedPlayer;
@optional
-(void)ViewController:(UIViewController *)sender
didSelectPlayer1:(Player *)selectedPlayer1;
@optional
-(void)ViewController:(UIViewController *)sender
didSelectPlayer2:(Player *)selectedPlayer2;
@end
playerCollectionViewController.h
#导入“CoreCollectionViewController.h”
#导入“Player.h”
@协议播放器集合视图控制器远程门;
@界面播放器CollectionViewController:CoreCollectionViewController
@属性(非原子,赋值)id委托;
@属性(非原子,强)NSString*titleText;
@属性(非原子,强)NSString*谓词;
-(iAction)取消:(UIBarButtonItem*)发送方;
-(IBAction)完成:(UIBarButtonItem*)发送方;
@结束
@协议播放机集合ViewControllerDelegate
@可选的
-(void)ViewController:(UIViewController*)发送方
didSelectPlayer:(Player*)selectedPlayer;
@可选的
-(void)ViewController:(UIViewController*)发送方
didSelectPlayer1:(播放器*)selectedPlayer1;
@可选的
-(void)ViewController:(UIViewController*)发送方
didSelectPlayer2:(播放器*)selectedPlayer2;
@结束
……以及执行:
#import "PlayersCollectionViewController.h"
#import "AppDelegate.h"
#import "PlayerCollectionViewCell.h"
@interface PlayersCollectionViewController ()
@property(nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@implementation PlayersCollectionViewController
@synthesize titleText, predicate;
static NSString * const reuseIdentifier = @"Cell";
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSManagedObjectContext*)managedObjectContext{
return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
NSError *error = nil;
if (![[self fetchedResultsController]performFetch:&error]) {
NSLog(@"Error: %@", error);
abort();
}
}
-(void)viewWillAppear:(BOOL)animated
{
[self setTitle:titleText];
[self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.fetchedResultsController sections]count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [[self.fetchedResultsController fetchedObjects]count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayerCollCell" forIndexPath:indexPath];
Player *player = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Configure the cell
if (player.playerImage) {
cell.playerImageView.image = [UIImage imageWithContentsOfFile:player.playerImageSmall];
}
[cell.playerNameLabel setText:player.firstName];
return cell;
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCollectionViewCell *selectedPlayerCell = (PlayerCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
[collectionView dequeueReusableCellWithReuseIdentifier:@"PlayerCollCell" forIndexPath:indexPath];
selectedPlayerCell.backgroundColor = [UIColor lightGrayColor];
Player *selectedPlayer = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Selected player %@", selectedPlayer.firstName);
if ([self.title isEqualToString:@"Select Player 1"]) {
[self.delegate ViewController:self didSelectPlayer1:selectedPlayer];
} else if ([self.title isEqualToString:@"Select Player 2"]) {
[self.delegate ViewController:self didSelectPlayer2:selectedPlayer];
} else if ([self.title isEqualToString:@"Select Player"]) {
[self.delegate ViewController:self didSelectPlayer:selectedPlayer];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - ViewController methods
- (IBAction)cancel:(UIBarButtonItem *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)done:(UIBarButtonItem *)sender { //not sure if this method and UI are needed...
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Fetched Results Controller Section
-(NSFetchedResultsController*) fetchedResultsController
{
if (_fetchedResultsController !=nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchedRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity =[NSEntityDescription entityForName:@"Player" inManagedObjectContext:context];
[fetchedRequest setEntity:entity];
NSSortDescriptor *lastNameSortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"lastName" ascending:YES];
NSSortDescriptor *firsttNameSortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"firstName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastNameSortDescriptor, firsttNameSortDescriptor, nil];
fetchedRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchedRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
#pragma mark - Fetched Results Controller Delegates
/*
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.collectionView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.collectionView endUpdates];
}
*/
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UICollectionView *collectionView = self.collectionView;
switch (type) {
case NSFetchedResultsChangeInsert:
[collectionView insertItemsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil]];
break;
case NSFetchedResultsChangeDelete: [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
break;
case NSFetchedResultsChangeUpdate: {
PlayerCollectionViewCell *selectedPlayerCell = (PlayerCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
//selectedPlayerCell.backgroundColor = [UIColor lightGrayColor];
Player *selectedPlayer = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (selectedPlayer.playerImage) {
selectedPlayerCell.playerImageView.image = [UIImage imageWithContentsOfFile:selectedPlayer.playerImageSmall];
}
[selectedPlayerCell.playerNameLabel setText:selectedPlayer.firstName];
}
break;
case NSFetchedResultsChangeMove: [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
[collectionView insertItemsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil]];
break;
}
}
@end
#导入“playerCollectionViewController.h”
#导入“AppDelegate.h”
#导入“PlayerCollectionViewCell.h”
@界面播放器集合视图控制器()
@属性(非原子,强)NSManagedObjectContext*managedObjectContext;
@属性(非原子,强)NSFetchedResultsController*fetchedResultsController;
@结束
@PlayerCollectionViewController的实现
@综合titleText、谓词;
静态NSString*const reuseIdentifier=@“Cell”;
-(instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(NSManagedObjectContext*)managedObjectContext{
返回[(AppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
}
-(无效)viewDidLoad
{
[超级视图下载];
//取消注释下一行以保留演示文稿之间的选择
//self.clearselectiononviewwillappear=否;
//注册单元类
[self.collectionView注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:reuseIdentifier];
//加载视图后执行任何其他设置。
n错误*错误=nil;
如果(![[self-fetchedResultsController]性能检测:&错误]){
NSLog(@“错误:%@”,错误);
中止();
}
}
-(无效)视图将显示:(BOOL)动画
{
[自我设置标题:标题文本];
[self.collectionView-reloadData];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
#布拉格标记
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView
{
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
{
返回[[self.fetchedResultsController fetchedObject]计数];
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
PlayerCollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“PlayerCollectionCell”forIndexPath:indexPath];
Player*Player=[self.fetchedResultsController对象索引路径:indexPath];
//配置单元格
if(player.playerImage){
cell.playerImageView.image=[UIImage-imageWithContentsOfFile:player.playerImageSmall];
}
[cell.playerNameLabel setText:player.firstName];
返回单元;
}
-(BOOL)collectionView:(UICollectionView*)collectionView应高亮度项索引路径:(NSI)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"checkedInStatus == 1"];