简体   繁体   中英

Switching between UITableViewController and UIViewController with table view inside

I am trying to transition from a UITableViewController to a UIViewController with a UITableView within it.

In my HomeViewController, I changed it from

HomeViewController: UITableViewController 

to

HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Now in storyboard, when I drag a Table View onto the View Controller, the TableView automatically resizes to the full screen... I made a reference to tableView in the Controller and set

tableView.delegate = self
tableView.datasource = self

but in HomeViewController when I print (self.view is UITableView) it prints true. When I tried to delete the TableView from the HomeVC in storyboard, and add a UIView instead, it wouldnt let me. I am confused why? How can I transition from TableView to TableView within UIViewController. Thanks!

UITableViewController is a subclass of UIViewController , in your strorybord you have built in UITableViewController that Manages a UITableView and its custom class should a subclass of UITableViewController . so the change you made to you custom class to a subclass of UIViewController will not work as you expected.

You need to drag a UIViewController then add UITableView as any view. then set custom class the one you have.

Also: .h

@interface HomeViewController : UIViewController 
//...
@end

.m

@interface HomeViewController ()<UITableViewDataSource,UITableViewDelegate> //implement Protocol
//...
@end

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