由于我无法真正描述此错误,因此我用错误记录了我的屏幕。 http://www.youtube.com/watch?v=w2FqKKcL2Ck&feature=youtu.be 基本上我不知道该怎么做。当用户完成“要完成的任务”部分中的所有任务时,我希望部分标题保留在那里......只是其中没有对象(只是因为它需要添加更多任务)。
幸运的是,当用户没有任何已完成的任务时,那里的标题会消失,这是应该发生的。我希望顶部标题不会消失...
此外,与删除相同的事情。当最后一个对象被删除时,我真的不希望“要完成的任务”部分完全消失。
我听说这是不可能的,除非您使用像TAFetchResultsController这样的自定义子类,但是我尝试使用它,但它对我来说太复杂了,无法实现(并且有点破坏了我的应用程序而不是修复它)。或许大家有什么建议?
以下是一些相关代码:
任务核心数据属性
@interface Tasks : NSManagedObject
@property (nonatomic, retain) NSString *sectionString;
@end
@implementation Tasks
@dynamic sectionString;
@end
将任务添加到不同的部分
NSManagedObjectContext *context = self.managedObjectContext;
NSManagedObject *startingTask = [NSEntityDescription insertNewObjectForEntityForName:@"Tasks" inManagedObjectContext:context];
[startingTask setValue:@"Eat Dinner" forKey:@"taskName"];
[startingTask setValue:[NSNumber numberWithDouble:400] forKey:@"timeInterval"];
[startingTask setValue:@"Tasks To Complete" forKey:@"sectionString"];
NSManagedObject *finishedTask = [NSEntityDescription insertNewObjectForEntityForName:@"Tasks" inManagedObjectContext:context];
[finishedTask setValue:@"Do Laundry" forKey:@"taskName"];
[finishedTask setValue:[NSNumber numberWithDouble:400] forKey:@"timeInterval"];
[finishedTask setValue:@"Completed Tasks" forKey:@"sectionString"];
NSError *error;
if (![context save:&error]) {
NSLog(@"couldn't save: %@", [error localizedDescription]);
}
表视图控制器.m:
-(void) viewDidLoad{
// ---Start Core Data With NSFetchedResultsController---
[super viewDidLoad];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]){
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
// ---End Core Data w/ NSFetchedResultsController---
[self.tableView setDelegate:self];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
holdViewsArray = [[NSMutableArray alloc]init];
UIView *seperatorView;
UIView *seperatorView2;
NSString *sectionTitle = @"Tasks To Complete";
NSString *section2Title = @"Completed Tasks";
UILabel *label = [[UILabel alloc]init];
UILabel *label2 = [[UILabel alloc]init];
label.frame = CGRectMake(10.0, 5.0, 320.0, 50.0);
label.text = sectionTitle;
label2.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
label2.text = section2Title;
headerView = [[UIView alloc]initWithFrame:label.frame];
headerView2 = [[UIView alloc]initWithFrame:label2.frame];
CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-2, 320, 1);
CGRect sep2Frame =CGRectMake(0, headerView2.frame.size.height-2, 320, 1);
seperatorView = [[UIView alloc] initWithFrame:sepFrame];
seperatorView2 = [[UIView alloc]initWithFrame:sep2Frame];
[headerView addSubview:seperatorView];
[headerView2 addSubview:seperatorView2];
[headerView addSubview:label];
[headerView addSubview:button];
[headerView2 addSubview:label2];
[holdViewsArray addObject:headerView];
[holdViewsArray addObject:headerView2];
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Tasks" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *isCompleted = [[NSSortDescriptor alloc]initWithKey:@"sectionString" ascending:NO];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"dateCreated" ascending:YES];
[fetchRequest setSortDescriptors:@[isCompleted, sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionString"
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Tasks *task = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = task.taskName.uppercaseString;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.0f", task.timeInterval];
cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"uncheckedhighlighted.png"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
if (indexPath.section == 1)
[cell.contentView setAlpha:0.5];
else {
[cell.contentView setAlpha:1];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlechecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cellSubclassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell)
cell = [[cellSubclassCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
Tasks *task = [_fetchedResultsController objectAtIndexPath:tappedIndexPath];
if ([task.sectionString isEqual: @"Tasks To Complete"]){
task.sectionString = @"Completed Tasks";
} else if ([task.sectionString isEqualToString:@"Completed Tasks"]){
task.sectionString = @"Tasks To Complete";
}
[self.tableView reloadData];
NSTimeInterval time = [[NSDate date] timeIntervalSinceReferenceDate];
[task setDateCreated:time];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [holdViewsArray objectAtIndex:0];
break;
case 1:
return [holdViewsArray objectAtIndex:1];
break;
}
return 0;
}