【问题标题】:How to add a UIViewController/UITabBar to a UITableViewCell?如何将 UIViewController/UITabBar 添加到 UITableViewCell?
【发布时间】:2014-11-12 00:59:55
【问题描述】:

我想将自定义 UITabBar 实现添加到 UITableViewCell

如果 UITableViewCell 本身就是 UIViewController 我可以使用视图控制器包含,但它不是。

我现在能做的最好的就是将TabBar's 视图添加到cellForRowAtIndexPath 中单元格的contentView

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    NSString *cellID;
    UITableViewCell *cell;

    cellID = @"TabCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellID] ?: [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    cell.contentView.backgroundColor = UIColor.blueColor;

    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    UIViewController *vc1 = [UIViewController new];
    vc1.view.backgroundColor = UIColor.magentaColor;
    vc1.title = @"TAB1";

    UIViewController *vc2 = [UIViewController new];
    vc2.view.backgroundColor = UIColor.purpleColor;
    vc2.title = @"TAB2";

    tabBarController.delegate = self;
    tabBarController.viewControllers = @[vc1, vc2];

    [cell.contentView addSubview:tabBarController.view];

    return cell
}

问题是单元格只显示TabBar 的视图,而不是TabBar 控件本身。

如何在UITableViewCell 中显示整个TabBar

【问题讨论】:

  • 你能截图看看现在单元格的样子吗
  • 将视图控制器放在表格视图单元格中的想法让我感到紧张。你能告诉我们你想要完成什么吗?

标签: ios uitableview uitabbar


【解决方案1】:

尝试使用容器视图,它允许您包含子视图控制器。 Here's 另一个 SO 帖子,其中包含有关如何执行此操作的说明。

但我必须同意@rikkigibson 在 cmets 上所说的话,也许你应该重新考虑你的方法。如果您分享有关您想要完成的任务的更多详细信息,也许我们可以提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多