我在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,而且我确实需要完成这个任务。我省略了额外的代码,因为它与问题无关。谢谢。 –