2014-05-10 28 views
-2

我有一個視圖控制器管理2個tableviews。我使用一個標誌來跟蹤選擇哪個表。在每個委託函數中,我只需檢查標誌並使用正確的表格。一個ViewController與2 TableViews - 應用程序崩潰

一切工作很好,除了當我加載第二個表具有較少的項目比第一個,崩潰時,我滾動表,爲以下錯誤。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0' 

*第一擲調用堆棧:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Drawing Row = %d Total num Of items = %d", indexPath.row, [[self.fetchedResultsControllerComments fetchedObjects] count]); 

打印此:

Drawing Row = 2 Total num Of items = 0 

如果項目在此表中的數字是正確的,那麼爲什麼這個函數獲取調用首先?

下面是代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(currentSelectionTableType1) 
    { 
     // Draw first kind of cell. 
     PlainImageCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"]; 
     if(cell1 == nil) 
      cell1 =[[PlainImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImageCell"]; 
     [self configureCell1:cell1 atIndexPath:indexPath]; 
     return cell1; 
    } 
    // else Draw the second kind of cell 
    PlainTextCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"TextCell"]; 
    if(cell2 == nil) 
     cell2 =[[PlainTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextCell"]; 
    [self configureCell2:cell2 atIndexPath:indexPath]; 
    return cell2; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(currentSelectionTableType1) 
     return [[self.fetchedResultsControllerDataSource1 sections] count]; 
    return [[self.fetchedResultsControllerDataSource2 sections] count]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo; 
    if(currentSelectionTableType1) 
    { 
     sectionInfo = [self.fetchedResultsControllerDataSource1 sections][section]; 
    } 
    else 
    { 
     sectionInfo = [self.fetchedResultsControllerDatasource2 sections][section]; 
    } 
    return [sectionInfo numberOfObjects]; 

} THX

+1

什麼ü需要的標誌?每個委託/數據源消息都會獲取它發送的表視圖。 – vikingosegundo

+0

顯示完整的'cellForRowAtIndexPath'方法,以便我們看到如何處理兩個表。同時顯示'numberOfSections'和'numberOfRowsInSection'方法。 – rmaddy

+0

@vikingosegundo這正是我的問題。這個函數甚至不會被調用,因爲它沒有在該索引處的單元格。 – hackerinheels

回答

1

編輯 - 根據您添加的代碼: 您需要您的條件來定義一個單元格,然後配置基礎上,該小區有條件的,然後返回條件後的單元格。如果您同時需要ImageView和TextCell,則可以在條件代碼中配置這些對象。

爲什麼不只是使用一個TableView和兩個數據源並根據需要切換數據源?

事情是這樣的:

@property(nonatomic, strong) NSArray *tableViewDataSource1; 
@property(nonatomic, strong) NSArray * tableViewDataSource2; 
@property(nonatomic) BOOL usingDataSource2; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.usingDataSource2) { 
     return [self.tableViewDataSource2 count]; 
    } 

    return [self. tableViewDataSource1 count];  
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Create the cell before conditional 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; 

    // Conditionally configure the cell 
    if (self.usingDataSource2) { 
     // Configure Cell using self.tableViewDataSource2 data 
    } else { 
     // Configure Cell using self.tableViewDataSource1 data 
    } 

    // Return the configured cell after the conditional 
    return cell; 
} 
+0

我不確定爲什麼人們低估了我的問題。我正在做你剛剛完成的工作。用細節編輯我的問題。 – hackerinheels

+0

我也不知道,但問題出在你的'cellForRowAtIndexPath'方法中。根據你添加到你的問題的代碼修改我的答案。 –