【发布时间】:2015-09-28 09:17:15
【问题描述】:
我在 Xcode 7 中有两个使用 Storyboard 的 UITableView。我已经使用 Connections Inspector 为两个表视图设置了委托和数据源。
让第一个表视图成为主表视图,并让主表视图的每个单元格中的表视图成为具有适当命名的单元格标识符的详细表视图和分别。
当[tableView dequeueReusableCellWithIdentifier:@"MainCell" forIndexPath:indexPath] 执行时,它会立即为DetailCell 调用其dataSource 方法-cellForRowAtIndexPath:,从而阻止我及时设置自定义实例变量以将适当的数据添加到每个单元格。
以下是使用 cmets 标记的简化示例。
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: 方法时,我没有机会为其数据源设置值,在本例中为additionalInfo。
【问题讨论】:
标签: ios objective-c uitableview