2015-09-28 59 views
1

我在Xcode 7中有兩個使用Storyboard的UITableViews。我已經使用Connections Inspector爲兩個表視圖設置了委託和數據源。在UITableViewCell中將UITableView嵌入到iOS 9,Xcode 7和Storyboard中

讓第一表視圖是主表視圖並讓主表視圖中的每個小區內的表視圖與適當且分別命名爲小區標識符的詳細表格視圖

[tableView dequeueReusableCellWithIdentifier:@"MainCell" forIndexPath:indexPath]執行時,它立即要求DetailCell阻止我在時間設置自定義實例變量到相應的數據添加到每個細胞其DataSource方法-cellForRowAtIndexPath:

以下是使用註釋標記的簡化示例。

MainTableViewController:

@implementation MainTableViewController 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Keep in mind the following two (2) lines are set using the Connections Inspector 
    //cell.detailTableView.dataSource = cell; 
    //cell.detailTableView.delegate = cell; 

    // Stepping over the following line will jump to the 
    // other `-cellForRowAtIndexPath:` (below) used to set 
    // the detail info. 
    cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MainCell" forIndexPath:indexPath]; 

    CustomObj *obj = self.mainData[indexPath.row]; 
    cell.nameLabel.text = obj.name; 
    cell.additionalInfo = obj.additionalInfo; // This line is not set before instantiation begins for the detail table view... 

    return cell; 
} 

... 

@end 

DetailTableViewCell(包含一個UITableView並實施適當的協議):

@interface DetailTableViewCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate> 
@property (nonatomic, weak) IBOutlet UILabel *nameLabel; 
@property (nonatomic, weak) IBOutlet UITableView *detailTableView; 
@property (nonatomic, strong) CustomObj *additionalInfo; 
@end 

@implementation DetailTableViewCell 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell = (DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath]; 

    // Instantiate detail ... 
    cell.detailLabel.text = self.additionalInfo.text; 

    // Problem! 
    // self.additionalInfo == nil thus we cannot set a value to the label. 

    return cell; 
} 

... 

@end 

問題是當細節-cellForRowAtIndexPath:方法被調用時,我還沒有機會爲其dataSource設置一個值,在本例中爲additionalInfo

回答

1

有很多可能的方法來解決你的問題,但首先我會說,你的設計似乎不是一個好的,一個UItableViewCell有另一個UITableView,和另一個UItableViewCell在這個UITableView內?你爲什麼這麼做?只需使用一個UITableView並將所有視圖放到一個UItableViewCell中,因爲子視圖應該足夠了。

現在得到您的問題:

我會建議不要使用IBOutlet中設立的委託和數據源,使用代碼。這可以讓你有機會在準備好時延遲設置dataSource和delgate。一旦你認爲這是適當的時候,只需要調用[cell.detailTableView reloadData]將觸發您DetailTableViewCell調用cellForRowAtIndexPath

@implementation MainTableViewController 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// Keep in mind the following two (2) lines are set using the Connections Inspector 
//cell.detailTableView.dataSource = cell; 
//cell.detailTableView.delegate = cell; 

// Stepping over the following line will jump to the 
// other `-cellForRowAtIndexPath:` (below) used to set 
// the detail info. 
cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MainCell" forIndexPath:indexPath]; 

CustomObj *obj = self.mainData[indexPath.row]; 

cell.nameLabel.text = obj.name; 
cell.additionalInfo = obj.additionalInfo; // This line is not set before instantiation begins for the detail table view... 

// setup dataSource and delegate now 
cell.detailTableView.dataSource = cell; 
cell.detailTableView.delegate = cell; 
// call reloadData whenever you think is proper 
[cell.detailTableView reloadData]; 

return cell; 
} 
+0

我忘了設置數據源,並使用代碼上的UITableView委託變量可以工作到我的優勢。我只有兩個層次的UITableViews,而且我確實需要完成這個任務。我省略了額外的代碼,因爲它與問題無關。謝謝。 –

-1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell* cell = nil; 
    //Check this call is for which table view. 
    if(tableView == detailTableView) { 
     cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MainCell" forIndexPath:indexPath]; 
     // Do any additional setup you want with MainCell 

    } else { 
     cell = (DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath]; 
     // Do any additional setup you want with DetailCell 

    } 

    return cell; 
}