【问题标题】:How do I delegate a UITableViewController with static cells which embed with a container inside UIViewController?如何委派 UITableViewController 与在 UIViewController 内嵌入容器的静态单元格?
【发布时间】:2016-07-17 20:39:01
【问题描述】:

我的故事板中有一个 UIViewController。在这个 UIViewController 里面,我有一个嵌入了 tableViewController 的容器。这个 tableViewController 是一个静态表。如何在 UIViewController 中使用这个 tableViewController?

我已经设置了类似UITableViewDelegate, UITableViewDataSource 的协议,但我不知道如何获取该 tableViewController 并委托给我的 UIViewController。

下面是屏幕。

附:我试图制作一个侧面导航栏。如果您需要查看任何代码,请通知我。

【问题讨论】:

  • 您可以使用 UIViewController 作为父 ViewController 和 tableViewController 作为 childViewController。但更好的解决方案是使用 UIViewController 中的 UITableView 作为 subView。
  • 因为是静态表,所以不能放到UIViewController中。我构建它时会出现错误。这就是我在 UIViewController 中放置一个容器视图并嵌入 tableViewController 的原因。似乎父 ViewController 方式会起作用。但是你能告诉我更多吗?
  • @SandeepKumar 感谢 Kumar,它就像一个魅力!

标签: ios objective-c uitableview uiviewcontroller delegates


【解决方案1】:

原来我必须在嵌入 tableViewController 中设置类并将[self addChildViewController: myTableViewController] 放在 UIViewController 类中。并且我可以在 UITableViewController 类中实现来控制它。

【讨论】:

    【解决方案2】:

    您可以使用 segue 访问 tableViewController。只要实现- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender,它就会在viewController的viewDidLoad之前被调用。看到这个答案:Access Container View Controller from Parent iOS

    【讨论】:

      【解决方案3】:

      这是我的观点:为您的 tableViewController 创建一个委托,例如

      @protocol SideTableViewContollerDelegate <NSObject>
      
      - (void)didClickCellOfIndexPath:(NSIndexPath *)indexPath;
      
      @end
      
      @interface SideTableViewController
      
      @property (weak, nonatomic) id<SideTableViewContollerDelegate> delegate;
      
      @end
      

      在你的 UIViewController 中,你将控制器设置为 tableViewController 的委托

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
          if ([segue.identifier isEqualToString:@"go table view"]) {
             SideTableViewController *stvc = (SideTableViewController *)segue.destinationViewController;
             stvc.delegate = self;
         }
      }
      

      在tableView的委托函数“didSelectRowAtIndexPath”内部做:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         [self.delegate didClickCellOfIndexPath:indexPath];
         [tableView deselectRowAtIndexPath:indexPath animated:YES];
      }
      

      剩下的就是在 uiview 控制器中实现 tableView 控制器的委托

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 2016-07-22
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        相关资源
        最近更新 更多