[英]Adding Custom UIView to UITableViewController
(iOS/Xcode Question) How do you programmatically add a custom UIView
(with labels and buttons) to a UITableViewController
? (iOS / Xcode问题)如何以编程方式将自定义UIView
(带有标签和按钮)添加到UITableViewController
? When I try to do this: 当我尝试这样做时:
CGFloat x = (self.tableView.bounds.size.width / 2);
CGFloat y = (self.tableView.bounds.size.height / 2);
NewView *newView = [[NewView alloc] initWithFrame:CGRectMake(x, y, 417, 228)];
[self.tableView addSubView:newView];
-all I get is a black rectangle. -我得到的只是一个黑色矩形。
*edit So I figured it out. *编辑,所以我想通了。 I forgot to load the nib. 我忘了装笔尖。
CGFloat x = (self.view.bounds.size.width / 2);
CGFloat y = (self.view.bounds.size.height / 2);
NewView *newView = [[[NSBundle mainBundle] loadNibNamed:@"NewView" owner:self options:nil] objectAtIndex:0];
newView.center = CGPointMake(x, y);
[self.tableView addSubview:newView];
The problem is your are trying to add an view to UITableView
, which works not as you expected. 问题是您正在尝试向UITableView
添加视图,该视图无法按预期工作。 UITableView
contains a set of UITableViewCell
. UITableView
包含一组UITableViewCell
。 Please read the documentation for UITableView, its delegate and data source. 请阅读UITableView的文档,其委托和数据源。
If you insist on using a UITableViewController along with a custom UIView, use UIViewController Containment. 如果坚持使用UITableViewController和自定义UIView,请使用UIViewController Containment。
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.