在UITableViewCell中将UITableView嵌入到iOS 9,Xcode 7和Storyboard中
我在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
。
有很多可能的方法来解决你的问题,但首先我会说,你的设计似乎不是一个好的,一个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;
}
- (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;
}
我忘了设置数据源,并使用代码上的UITableView委托变量可以工作到我的优势。我只有两个层次的UITableViews,而且我确实需要完成这个任务。我省略了额外的代码,因为它与问题无关。谢谢。 –