gpt4 book ai didi

ios - UITableView - 从单独的类重新加载数据

转载 作者:行者123 更新时间:2023-11-29 11:11:48 27 4
gpt4 key购买 nike

我搜索并查看了很多类似的问题,但仍然找不到答案。如何在不初始化它的另一个实例的情况下引用不同的类?如何从单独的类中调用“reloadData”以反射(reflect) MOC 的数据。MOC 似乎正在保存,因为我已经通过 NSNotification 验证了它。

弹出框类:

-(void)actionSave:(id)sender {
MainContent *entityContent =
[NSEntityDescription insertNewObjectForEntityForName:@"MainContent"
inManagedObjectContext:self.mDoc.managedObjectContext];
entityContent.date = [NSDate date];
entityContent.title = self.titleName.text;

//Main Question: How do I call the ViewController's function that is in a separate class?
[ViewController reloadTableView]; //???

//Separate Question: How do I dismiss this popover from inside of this function?
}

View Controller 类:

-(void)setupTable {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(gridV1, 140, column1_width, 768-170) style:UITableViewStylePlain];
[self.tableView setBackgroundColor:[UIColor clearColor]];

self.tableView.autoresizesSubviews = YES;

self.tableView.delegate = self.tableViewController;
self.tableView.dataSource = self.tableViewController;
[self.view addSubview:self.tableView];
}
-(void)reloadTableView{
[self.tableView reloadData];
}

TableViewController 类:(self.tableView 的委托(delegate)和数据源)

- (void)setupFetchedResultsController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MainContent"];

// I want to sort them by the "date" attribute input by [NSDate date].
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

// no predicate because we want ALL of the data in the "MainContent" entity.

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.mDoc.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
- (void)setMDoc:(UIManagedDocument *)mDoc {
if (_mDoc != mDoc) _mDoc = mDoc;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.mDoc) {
[[globalMDoc sharedDocumentHandler] performWithDocument:^(UIManagedDocument *coreDatabase) {
self.mDoc = coreDatabase;
[self setupFetchedResultsController];
}];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Overview Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
MainContent *content = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = content.title;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [content date]];

return cell;
}

最佳答案

好吧,像这个链接中的例子一样制作一个单例:http://www.galloway.me.uk/tutorials/singleton-classes/

然后在 UITableView 所在的 UIViewController 的 Singleton 中创建一个 ivar。然后在 UITableView View 的 VDL 中,执行类似

[单例 sharedSingleton].theViewController = self;

然后在另一个 View 中做类似的事情

    Singleton *singleton = [Singleton sharedSingleton];
[singleton.mytableview reloadData];

但是,对于 (= self) 行,您可能希望在更早的地方执行此操作,例如 applicationDidFinishLaunching,以便当您尝试访问它时 theViewController 变量不为 nil。

关于ios - UITableView - 从单独的类重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11218321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com