【问题标题】:uitableview cell value not refresh according with the ones in coredata after changeuitableview 单元格值在更改后不会根据 coredata 中的值刷新
【发布时间】:2014-04-28 15:16:53
【问题描述】:

我正在构建主从应用程序,说主视图A(TableView),第二视图(tableview)B,细节视图C。数据来自核心数据。在 secondview(tableview) 中,当用户点击特定单元格时,popover 将提示单元格值更改。在弹出屏幕上更改后,关闭弹出窗口,它返回到 secondview(tableview) 并相应地更新核心数据。并且单元格值有望反映更新值。 问题是它不能那样工作。单元格值仍然是旧值,尽管核心数据已更新为新值(我通过 NSLOG 在方法 --tableview:cellForRowAtIndexPath 中确认了这一点。它打印单元格的新值,但显示单元格的旧值)。我试图将 table-view:reloaddata 放入 viewwillappear 和 popoverControllerDidDismissPopover 方法,但它仍然不起作用。

我想不通。 secondView(tableView) 上的单元格值只有在我将视图返回到主视图 A 并再次将其切换到第二视图 B 后才会更新。

但是,第二个视图会立即反映新添加的单元格,添加也是通过弹出框提示单元格新值输入。

有人可以帮忙吗?谢谢。

-(void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{

    //get text from popover based on various class type

   if([popoverController.contentViewController isKindOfClass:[changecellvalueViewController class]])
   {
       NSString *thenewinputcellvalue =((changecellvalueViewController *)popoverController.contentViewController).thecellvalue.text;
       NSString *oldcellvalue=((changecellvalueViewController *)popoverController.contentViewController).thecellvalueold.text;
       if (0 != [ thenewinputcellvalue length] && oldcellvalue  !=  thenewinputcellvalue)     {

           NSManagedObjectContext *acontext =  [self.masterviewmasterController.fetchedResultsController managedObjectContext];

           NSManagedObject *cellTobeChanged = ((changecellvalueViewController *)popoverController.contentViewController).cellobjecttobechanged;
           [ cellTobeChanged setValue: thenewinputcellvalue forKey:@"cellvalue"];
           // Save the context.
           NSError *error = nil;
           if (![acontext save:&error])
           {
               // Replace this implementation with code to handle the error appropriately.
               // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
               NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
               abort();
           }


          if (![self.masterviewmasterController.fetchedResultsController performFetch:&error]) {
          // Replace this implementation with code to handle the error appropriately.
          // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
              NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
              abort();
          }

          [self.tableView reloadData];
        }
     }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell” forIndexPath:indexPath];

    // Configure the cell...
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@“cell”];
    }
    NSManagedObject *celllist =  [[SELF SORTCELLS] objectAtIndex:indexPath.row];
    cell.textLabel.text =   [[celllist valueForKey:@"cellName"] description];
    NSLog(@"cell text %@", cell.textLabel.text);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
  • (void)viewDidLoad { [超级viewDidLoad];

    // 取消注释以下行以保留演示文稿之间的选择。 // self.clearsSelectionOnViewWillAppear = NO;

    // 取消注释以下行以在此视图控制器的导航栏中显示一个编辑按钮。 // self.navigationItem.rightBarButtonItem = self.editButtonItem; [self.tableView registerClass:[UITableViewCell 类] forCellReuseIdentifier:@"Cell"]; [self.tableView reloadData]; }

【问题讨论】:

  • 你能粘贴几行你的代码吗?没有人可以帮助我想
  • 检查coreData是否有值

标签: ios objective-c core-data uitableview


【解决方案1】:

请检查您是否正在更新您的第二个视图的 UITableView 的数据源。 您可以通过

刷新该单元格值
  1. 使用更新的数据源重新加载该单元格(我猜您的输入弹出框正确更新了 UITableView 的数据源)使用方法 -

[tableview reloadRowsAtIndexPaths:withRowAnimation:]

  1. 当使用 UITableViewCell 中的 popoverControllerDidDismissPopover 委托关闭输入弹出框时 您可以使用弹出窗口中的值更新标签

【讨论】:

  • 我在想是否有东西在重新加载时缓存单元格值
  • 尝试调试它..您可以在输入弹出框被关闭后检查是否在表格单元格中调用委托?
  • 我调试它。在 cellforrowatindexpath 中,它返回新的单元格值。但我不明白为什么它仍然在屏幕上显示旧值。
  • 记录您的表格视图对象。如果您意外创建了两个 UItableView 对象(可能在 nib 中,一个来自代码),这就是为什么我正确调用所有方法但不更新 UI 的原因。
【解决方案2】:

您的问题看起来是您用来填充表格视图单元格的逻辑。这一行:

NSManagedObject *celllist = [[[(NSSet *) self.celltypes valueForKey:@"tocellList" ] allObjects] objectAtIndex:indexPath.row]; 

正在从集合中获取一个数组 (allObjects)。一个集合没有顺序,所以每次你请求这个数组时,它都会有不同的顺序。这意味着您的单元格没有确定性的内容,您将有重复的行和缺失的行。

每当集合发生变化时,您应该获取内容数组,对其进行排序并存储它。这个排序后的数组应该用作表格视图的数据源。

【讨论】:

  • 感谢您的回复。即使是重复或缺少行的情况。它应该可以反映在屏幕上,对吧?我厌倦了整理它。还是有问题
  • 如果您的日志显示正确的测试,那么它应该显示在屏幕上。还要记录索引路径,以便您知道它在列表中的位置。
猜你喜欢
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 2021-04-10
  • 2018-01-16
相关资源
最近更新 更多