简体   繁体   English

IOS / Xcode / Coredata:等效于[[self tableView] reloadData]; 什么时候没有tableview?

[英]IOS/Xcode/Coredata: What is equivalent of [[self tableView] reloadData]; when there is no tableview?

There must be a way to update a UIViewController or view in the absence of a tableView using NSFetchedResultsController? 必须有一种方法可以在没有tableView的情况下使用NSFetchedResultsController更新UIViewController或视图吗?

In this case there are some labels and a picture. 在这种情况下,有一些标签和图片。 I just want to refresh them with the latest data from managedobjectcontext. 我只想使用托管对象上下文中的最新数据刷新它们。

Thank you. 谢谢。

[myView setNeedsDisplay]; 

will redraw the view. 将重绘视图。

[myView setNeedsLayout];

will update the layout. 将更新布局。

Edit: After reading the comments, it's clear what you're trying to do. 编辑:阅读评论后,很清楚您要执行的操作。 It's called UI Data bindings and it's not natively supported in iOS. 这称为UI数据绑定 ,iOS本身不支持。 You can search github for possible open source implementations, but I can't guarantee or recommend any since I haven't used any. 您可以在github上搜索可能的开源实现,但是由于我没有使用过,所以我不能保证或推荐任何实现。 My suggestion is to encapsulate the pulling and displaying of data in a function and simply call it to reload everything, especially if the reloading of the data is a result of an event: 我的建议是将数据的提取和显示封装在一个函数中,并简单地调用它以重新加载所有内容,尤其是如果数据重新加载是事件的结果:

-(void)loadDataFromDatabaseAndDisplayIt{
      NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
      NSEntityDescription* entity = [NSEntityDescription
                  entityForName:@"User" inManagedObjectContext:self.context];
     [fetchRequest setEntity:entity];
      NSArray* data = [self.context executeFetchRequest:fetchRequest error:&error];

      User* firstUser = data[0];

      self.labelName.text = firstUser.name;
      self.labelPhoneNumber.text = firstUser.phoneNumber;
      //...
}

-(void)viewDidLoad{
     //...
     [self loadDataFromDatabaseAndDisplayIt];
}

-(void)someEventDidOccurThatShouldReloadData:(id)sender{
     [self loadDataFromDatabaseAndDisplayIt];
}

What a table view's reloadData does is it calls eg cellForRowAtIndexPath a number of times, which is where you yourself add code to set the content of your labels, etc. 表视图的reloadData作用是多次调用例如cellForRowAtIndexPath ,这是您自己添加代码以设置标签内容的地方,等等。

Similarly, you yourself will have to set the content of the labels and other UI outlets. 同样,您自己也必须设置标签和其他UI插座的内容。 setNeedsDisplay will only redraw the screen, not update your UI elements with data. setNeedsDisplay将仅重绘屏幕,而不用数据更新UI元素。 You will have to do this "manually" in your controller. 您将必须在控制器中“手动”执行此操作。

Make a method called something like reloadViewData and reset all the labels with the current data object(s). 制作一个称为reloadViewData类的方法,并使用当前数据对象重置所有标签。 This is not much work because presumably you have already written this code for the initial view setup. 这项工作并不多,因为大概您已经为初始视图设置编写了此代码。 Just factor it out into this method and call it both for view setup and view update. 只需将其分解为此方法,然后为视图设置和视图更新调用它即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM