我试图完成的是UITableView快速重新加载。每个单元格都有“检查”UIButton,告诉用户该单元格的项目被选中。所有选中的单元格应该位于列表的最后(底部单元格)。无动画重新加载UITableView
我使用NSFetchResultsController作为UITableView的委托和数据源。 NSFetchResultsController设定当用户敲击的UIButton在细胞更改与sectionKey“项”实体选择为“检查”的属性(部分OD“项”实体哪个)
- (id)initWithItemView:(ItemView*)iv{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[CoreDataHandler context]];
[request setEntity:entity];
NSArray *predicates = [NSArray arrayWithObjects:[iv listDBUsingContext:[CoreDataHandler context]],nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lista IN %@ AND deletedDB == NO", predicates];
[request setPredicate:predicate];
[request setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checked" ascending:YES selector:@selector(compare:)];
NSSortDescriptor *sortDescriptor2
= [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES selector:@selector(compare:)];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
[sortDescriptor2 release];
if (self=[[FastTableDelegate alloc]
initWithFetchRequest:request
managedObjectContext:[CoreDataHandler context]
sectionNameKeyPath:@"checked"
cacheName:nil])
{
self.delegate = self;
self.itemView = iv;
self.heightsCache = [NSMutableDictionary dictionary];
}
[request release];
[self performFetch:nil];
return self;
}
然后操作“选中”属性在NSmanagedObject(Item实体)并保存上下文。然后我NSFetchReslutsController被告知一个项目改变了它的状态,并进行使用功能
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.itemView.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
}
}
因为我改变了“选中”属性,该属性是sectionKey我NSFetchedResultsController的UITableView应该与这种情况下
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
重装的UITableView重装
这是一个问题,因为当单元格被插入或者被拖拽时,UITableView执行的动画持续时间大约是0.5s。在这段时间里,UITableView停止为下一个单元格收集事件,因此当用户“检查”另一个单元格时,它将不会被通知该点击。我试图实现的是让用户快速检查一些单元而不用等待动画结束。
可能的解决方案:
deleteRowsAtIndexPaths/insertRowsAtIndexPaths使用reloadData istead。这将是没有动画,但重新加载所有UITableView更长时间重新加载一个单元格,所以在我的情况下,用户将等待UITableView重新加载 - 再次等待。有没有一种方法可以避免重新加载所有UITableView动画?
使用户能够检查单元格,但仅在上次单元格选择后的0.5s之后保存上下文。用户可以选择快速多细胞,当他最终选择的所有更改会传播到UITableView中 - 我的老板希望每一个选定单元第二部分发送没有再分组,选择一个重载的每个细胞 - 再坏主意
也许一些自定义tableView(用户创建,不是苹果)?
主要目标:
允许用户“检查”每一个细胞“检查”
后快速刷新的UITableView我打开的任何理想:)
我觉得我们误会了对方。 viewDidAppear:当视图呈现给用户时被触发,但是当用户正在“检查”单元格时,我不会呈现任何新视图,而是我想顺利地重新加载现有的UITableView – pawelini1 2012-04-12 12:22:40
,然后重新加载表中的didselect /检查单元格。 – Ranga 2012-04-17 06:00:28