简体   繁体   中英

Adding Custom UIView to UITableViewController

(iOS/Xcode Question) How do you programmatically add a custom UIView (with labels and buttons) to a 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 contains a set of UITableViewCell . Please read the documentation for UITableView, its delegate and data source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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