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