我搜索並查看了許多類似的問題,但仍無法找到答案。 如何在不初始化其他實例的情況下引用不同的類? 如何從單獨的類中調用「reloadData」以反映MOC的數據。 MOC似乎正在保存,因爲我通過NSNotification驗證了它。UITableView - 從單獨的類reloadData
酥料餅類:
-(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?
}
視圖控制器類:
-(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的委託和數據源)
- (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;
}
使用單身,這樣它不會被初始化兩次。 –
我很難將UITableView轉換爲單例,initWithFrame函數會丟失......是否有解決方法? – Jeremi
如何將TableViewController委託和數據源化到這個單例中?將它作爲子類來分類...? – Jeremi