1

好吧,我已经在这几天了,似乎无法弄清楚我做错了什么。

我想要的是在选择一行时更新核心数据。我把它记下来了,但是我希望刷新表格视图以显示更改。那就是我遇到问题的地方。我跑:

    [NSFetchedResultsController deleteCacheWithName:@"Root"];
    fetchedResultsController = nil;
    NSError *error = nil;
    if(![[self fetchedResultsController] performFetch:&error]){

    }
    [[self tabVe] reloadData];

更新数据后,但tableview没有更新。我会改变观点并回来,但它是......卡住了。我仍然可以滚动并选择更多行,但表格永远不会刷新。我有一个分段控制器,它可以很好地按照我想要的方式更改数据,但是我说表格卡住的原因是因为在我选择一行后,分段控制器不会像它一样更改表格的内容将在选择一行之前。

我不是在编辑模式下这样做,我只是希望它在被选中时更新一个值。这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self.tabVe deselectRowAtIndexPath:indexPath animated:YES];
        if(cyc == 1){
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                Items *anItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
                anItem.need = @"0";
                NSError *error = nil;
                if(![managedObjectContext save:&error]){

                }
                [NSFetchedResultsController deleteCacheWithName:@"Root"];
                fetchedResultsController = nil;
                NSError *error = nil;
                if(![[self fetchedResultsController] performFetch:&error]){

                }
                [[self tabVe] reloadData];
            }
        }
    }

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"name"] description];
    cell.detailTextLabel.text = [[managedObject valueForKey:@"notes"] description];
    if([[managedObject valueForKey:@"need"] description] == @"0"){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else if([[managedObject valueForKey:@"need"] description] == @"1"){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

我希望有人可以帮助我解决这个问题。cyc 是一个 NSInteger 显示分段控制器的当前位置,我目前仅在 cyc 等于 1 时更新项目。

编辑:

一切顺利,直到

if(![managedObjectContext save:&error]){

}

正在运行。在那之后,那是桌子似乎卡住的时候。

4

1 回答 1

3

只是猜测,但有两件事浮现在脑海:

  1. 根据我的经验,尝试减少代码,只需将其设置为并让延迟加载处理获取didSelectRowAtIndexPath:就足够了。另一个想法是根本不使用任何缓存。这值得一试,如果它不影响性能,当然是合理的。fetchedResultsControllernil

  2. 我注意到您正在将两个字符串与==. 我认为您应该使用该isEqualToString:方法进行此比较。另外,我不确定为什么description除了字符串之外还需要该方法- descriptionanNSString的字符串完全相同。尝试简化这一点。

此外,从您的代码中不清楚变量cyc是什么以及它来自哪里。您可能希望选择更易于人类理解的变量名称。

于 2011-08-24T09:17:49.543 回答