【问题标题】:Embed UITableView in UITableViewCell in iOS 9, Xcode 7 and Storyboards在 iOS 9、Xcode 7 和 Storyboard 中的 UITableViewCell 中嵌入 UITableView
【发布时间】: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


    【解决方案1】:

    有很多可能的方法来解决你的问题,但首先我要说的是,你的设计似乎不是一个好的,一个 UItableViewCell 有另一个 UITableView,这个 UITableView 里面还有另一个 UItableViewCell?你为何这样做?只需使用一个 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;
    }
    

    【讨论】:

    • 我忘记了使用代码在 UITableView 上设置数据源和委托变量可能对我有利。我只有两个层次的 UITableViews,我发现它对于这个任务是必要的。我省略了额外的代码,因为它与问题无关。谢谢。
    【解决方案2】:
    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 2016-01-13
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      相关资源
      最近更新 更多