2012-04-17 52 views
1

我在一個應用程序中有2個控制器,因爲我正在嘗試這些不同的設置。UITableViewController vs UITableView

一個是UITableViewController,它的方式我明白委託和數據源是從超類繪製的。

現在按照這個理論,我現在也有一個UIViewController和一個UITableView,並在頭文件中聲明瞭數據源和委託。

到目前爲止好:-)

現在,這是位我不明白:

在實現文件中我可以使用的tableView指針鏈接的數據,但我使用的核心數據和我有這樣一段代碼除其他:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
    { 
    [self.tableView beginUpdates]; 
    } 

這和5其他情況下給出的UIViewController的一個錯誤,如果我不設置一個

IBOutlet UITableView *tableView; 

當然是:-)

合成的,但如果我聲明這的tableView我得到一個警告就行了雙了,說這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
[self configureCell:cell atIndexPath:indexPath]; 
return cell; 
} 

,因爲它也聲明瞭的tableView。

所以我有2個問題:

1)爲什麼我設置IBOutlet中,如果我設置了一個UITableViewDatasource和代表? 2)如果我給UITableViewCell一個不同的指針名稱 - 比如tableView2,爲什麼我在編輯數據時看不到更改,但只能在重新啓動應用程序後看到結果?

MasterViewController,它是UITableViewController沒有這個選項。

乾杯傑夫

---添加圖像----

enter image description here

enter image description here


要進入編輯模式,我有這樣的代碼在viewDidLoad中

self.navigationItem.leftBarButtonItem = self.editButtonItem; 

,我使用此代碼動作需要做什麼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

{ 如果(editingStyle == UITableViewCellEditingStyleDelete){ 的NSManagedObjectContext *上下文= [self.fetchedResultsController managedObjectContext]; [context deleteObject:[self。fetchedResultsController objectAtIndexPath:indexPath]];

NSError *error = nil; 
    if (![context 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(); 
    } 
} 
} 

現在+選項工作,但我只看到結果後,我重新啓動應用程序。我可以輕掃並刪除,但它不會在應用程序的同一個實例中進行動畫生活。

回答

1

你已經聲明瞭一個名爲tableView的屬性,據推測它只是用相同的名稱合成它。

UITableViewController也有一個名爲的tableView財產,但後盾伊娃將被稱爲不同的東西,大概_tableView,正是因爲默認的tableview的數據源和委託方法通常是在一個參數傳遞被稱爲tableView其中,在方法的範圍,與你的實例變量衝突。

那個方法裏面,tableView是什麼意思?通過的論據,還是你的伊娃?

在回答您的具體問題:

  1. 你需要爲這些情況下出口時,您的視圖控制器需要交談的表視圖(當你告訴它開始更新等)。當表視圖需要與視圖控制器交談時(如詢問單元格時),數據源和委託連接就在那裏。

  2. 我不確定你在這裏問什麼,你可以打電話給表格查看任何你喜歡的東西,但你也必須改變發送消息到self.tableView的一切。對於consitency我將綜合屬性,像這樣:

    @synthesize tableView = _tableview; 
    

    這可以防止所謂tableView現有首先伊娃。我假設你沒有申報的伊娃,並且只宣佈屬性作爲一個IBOutlet ?:

    @property (nonatomic) IBOutlet UITableView *tableView; 
    
+0

嗨,感謝您的答覆1)我已經宣佈伊娃,然後設置一個屬性它匹配。 2)我想簡化問題,如果我在頭文件中設置TableView委託和數據源,爲什麼不與tableView參數一起來? 3)我已經嘗試過使用_tableView方法進行合成,但是與將「cellForRowAtIndexPath:」更改爲tableView2的效果相同。 4)當我添加和編輯UITableViewController時,結果動畫,例如刪除標誌出現在顯示中,當我重命名View Controller中的UITableView時,這種情況不會發生。 – 2012-04-17 09:24:53

+0

對不起,這是一個很長的評論,只是試圖用我的解釋澄清你的問題;-) – 2012-04-17 09:25:33

+0

你可能需要在你的問題中包含更多的代碼。 1)不要這樣做,如我所建議的,去掉伊娃的聲明。 2)我不知道你是什麼意思,認爲顯示相關的代碼3)你需要包括你現在的代碼4)見3)。你如何將視圖控制器連接到表視圖,以及如何將表視圖的數據源和委託連接到視圖控制器? – jrturton 2012-04-17 09:29:27

相關問題