2012-06-27 44 views
1

我搜索並查看了許多類似的問題,但仍無法找到答案。 如何在不初始化其他實例的情況下引用不同的類? 如何從單獨的類中調用「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; 
} 
+0

使用單身,這樣它不會被初始化兩次。 –

+0

我很難將UITableView轉換爲單例,initWithFrame函數會丟失......是否有解決方法? – Jeremi

+0

如何將TableViewController委託和數據源化到這個單例中?將它作爲子類來分類...? – Jeremi

回答

1

好了,所以做出這樣從這個鏈接的例子一個單:http://www.galloway.me.uk/tutorials/singleton-classes/

然後在UITableView中的VDL使辛格爾頓伊娃您的UIViewController您的UITableView在隨後。看來,這樣做

在其他視圖[Singleton sharedSingleton].theViewController = self;

然後像做

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

但是,對於(= self)行,您可能希望在applicationDidFinishLaunching之前的某個位置執行此操作,以便當您嘗試訪問它時,該ViewController變量不爲零。

1

我個人使用NSNotifcations,我有一個幾個異步發生的事件,我發回一個通知。

也就是說,我所做的是在後臺線程上下載圖片,並在完成後重新加載表格。我通過向tableviewclass發送通知來完成此操作,並且表視圖類會重新加載其自己的tableview。

〜丹