Iphone NSFetchResultsController对象重新设计
我有两种观点。一个是使用NSFetchResultsController显示核心数据中的购物列表。第二个使用简单的NSFetchRequests显示列表中的项目。两个视图都包含UITableView 启动应用程序时,我创建NSFetchResultsController(由ActiveFetchResults子类)Iphone NSFetchResultsController对象重新设计,iphone,objective-c,core-data,nsfetchedresultscontroller,Iphone,Objective C,Core Data,Nsfetchedresultscontroller,我有两种观点。一个是使用NSFetchResultsController显示核心数据中的购物列表。第二个使用简单的NSFetchRequests显示列表中的项目。两个视图都包含UITableView 启动应用程序时,我创建NSFetchResultsController(由ActiveFetchResults子类) -(id)initActiveFetch{ NSFetchRequest*request=[[NSFetchRequest alloc]init]; NSEntityDescript
-(id)initActiveFetch{
NSFetchRequest*request=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“Lista”inManagedObjectContext:[CoreDataHandler context]];
[请求集合实体:实体];
NSPredicate*谓词=[NSPredicate谓词WithFormat:@“存档==0,已删除==否”];
[请求集谓词:谓词];
NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“位置”升序:否];
NSArray*sortDescriptors=[[NSArray alloc]initWithObjects:sortDescriptor,nil];
[请求集合描述符:排序描述符];
[sortDescriptors发布];
[sortDescriptor发布];
if(self=[[ActiveFetchResults alloc]
initWithFetchRequest:请求
managedObjectContext:[CoreDataHandler上下文]
sectionNameKeyPath:无
缓存名称:nil])
{
self.delegate=self;
}
[请求释放];
回归自我;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[[self sections]count];
}
-(NSInteger)表格视图:(UITableView*)表格行数节:(NSInteger)节{
id sectionInfo=[[self sections]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-(NSInteger)区段中的行数{
id sectionInfo=[[self sections]objectAtIndex:0];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
ActiveListsCell*单元;
cell=(ActiveListsCell*)[tableView dequeueReusableCellWithIdentifier:@“ActiveLists”];
如果(单元格==nil){
cell=[[[ActiveListsCell alloc]initWithFrame:CGRectZero重用标识符:@“ActiveLists”]自动释放];
}
NSManagedObject*managedObject=[self-objectAtIndexPath:indexath];
[cell setLista:(Lista*)managedObject];
NSLog(@“%@,%@,%d”,managedObject,[managedObject名称],[managedObject项目]计数];
cell.activelistselegate=self;
[cell.roundedView setAlpha:1.0];
cell.button.alpha=1.0;
cell.counter.alpha=1.0;
返回单元;
}
-(NSString*)tableView:(UITableView*)tableView标题标题标题标题部分:(NSInteger)部分{
id sectionInfo=[[self sections]objectAtIndex:section];
返回[sectionInfo name];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图{
返回[自分段索引];
}
-(NSInteger)tableView:(UITableView*)分区索引的tableView部分exttitle:(NSString*)标题索引:(NSInteger)索引{
return[self-sectionForSectionIndexTitle:title-atIndex:index];
}
-(void)controllerWillChangeContent:(NSFetchedResultsController*)控制器{
[self.tableView开始更新];
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(NSInteger)ChangeType:(NSFetchedResultsChangeType)类型{
开关(类型){
案例NSFetchedResultsChangesInsert:
[self.tableView insertSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
}
}
//-(布尔)性能蚀刻:(N错误**)错误{
//ActiveFetchResults*ss=self;
//self=[self initActiveFetch];
//
//self.listView=ss.listView;
//self.listView.activeFetch=self;
//[self.listView.tableView setDelegate:self];
//[self.listView.tableView setDataSource:self];
//
//[释放党卫军];
//返回[超级性能蚀刻:错误];
//}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeObject:(id)一个对象
atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)类型
newindepath:(nsindepath*)newindepath{
UITableView*tableView=self.tableView;
开关(类型){
案例NSFetchedResultsChangesInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[tableView DeleteRowsatindExpath:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeUpdate:
if([anObject isKindOfClass:[Lista class]])){
Lista*Lista=(Lista*)一个对象;
如果([[lista deleted]布尔值]){
[tableView DeleteRowsatindExpath:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
其他的
[self-configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexath];
打破
案例NSFetchedResultsChangeMove:
[tableView DeleteRowsatindExpath:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
-(id)initActiveFetch{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lista" inManagedObjectContext:[CoreDataHandler context]];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"archive == 0 AND deleted == NO"];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
if (self=[[ActiveFetchResults alloc]
initWithFetchRequest:request
managedObjectContext:[CoreDataHandler context]
sectionNameKeyPath:nil
cacheName:nil])
{
self.delegate = self;
}
[request release];
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self sections] count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSInteger)numberOfRowsInSection{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:0];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ActiveListsCell* cell;
cell = (ActiveListsCell*)[tableView dequeueReusableCellWithIdentifier:@"ActiveLists"];
if (cell == nil) {
cell = [[[ActiveListsCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"ActiveLists"] autorelease];
}
NSManagedObject *managedObject = [self objectAtIndexPath:indexPath];
[cell setLista: (Lista*)managedObject];
NSLog(@"%@, %@, %d ",managedObject, [managedObject name], [[managedObject items] count]);
cell.activeListsDelegate = self;
[cell.roundedView setAlpha:1.0];
cell.button.alpha = 1.0;
cell.counter.alpha = 1.0;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self sectionForSectionIndexTitle:title atIndex:index];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
//-(BOOL)performFetch:(NSError **)error{
// ActiveFetchResults* ss = self;
// self = [self initActiveFetch];
//
// self.listView = ss.listView;
// self.listView.activeFetch = self;
// [self.listView.tableView setDelegate:self];
// [self.listView.tableView setDataSource:self];
//
// [ss release];
// return [super performFetch:error];
//}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
if ([anObject isKindOfClass:[Lista class]]) {
Lista* lista = (Lista*)anObject;
if ([[lista deleted] boolValue]) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
else
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
-(void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath{
ActiveListsCell* activeCell = (ActiveListsCell*)cell;
NSManagedObject *managedObject = [self objectAtIndexPath:indexPath];
[activeCell setLista: (Lista*)managedObject];
}