简体   繁体   English

如何在自定义 UIView 中添加 UITableView?

[英]How to add UITableView inside custom UIView?

I want to create common custom view with uitableview.我想用 uitableview 创建通用的自定义视图。 I can do it using view controller and add subview as follows.我可以使用视图控制器来做到这一点,并按如下方式添加子视图。

ViewController *vwCon=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
vwCon.view.center=self.view.center;
[self.view addSubview:vwCon.view];

But after scrolling table view app become crash.但是在滚动表视图应用程序后变得崩溃。 So how can I add uitableview inside custom UIView?那么如何在自定义 UIView 中添加 uitableview 呢?

You shouldn't add a view controller as a subview - in fact Im not sure whether you are allowed to at all.你不应该添加一个视图控制器作为子视图 - 事实上我不确定你是否被允许。 You need to present a view controller modally, or push it as part of a navigation controller.您需要以模态方式呈现视图控制器,或将其作为导航控制器的一部分推送。 I doubt you really want that though- instead just use a UIView here rather than a view controller.我怀疑你真的想要那个 - 而不是在这里使用 UIView 而不是视图控制器。 Then you can add that as a subview to another view.然后您可以将其作为子视图添加到另一个视图。

You also mention tableviews though.你也提到了tableviews。 Have you looked at the storyboard?你看过故事板吗? Its easiest to do all that in there - just drag a UITableview object from the objects list on the right hand side, into the main view controller.最容易在那里完成所有这些 - 只需将 UITableview 对象从右侧的对象列表中拖到主视图控制器中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM