初入iOS开发,在使用TableView 的时候,发现有两种创建 ViewController 的方式。
方式一:直接继承自 UIViewController
#import <UIKit/UIKit.h>
@interface FisrtViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
方法二:继承自 UIViewController
#import <UIKit/UIKit.h>
@interface FirstViewController : UITableViewController
@end
两者的区别在于,UITableViewController 帮忙做了一些 UIViewController 没有做的事情,如下:
- Defines and setups up theUITableView.
- Sets itself as the table view's data source and delegate.
- Overrides thesetEditing:animated: method to also set theediting property of the table view.
- Deselects the last selected row in theviewWillAppear: method depending on theclearsSelectionOnViewWillAppear property.
- Flashes the table view's scrollbars in the viewDidAppear: method.
- Hooks up the refresh control (as of iOS 6).
- Reloads the table view the first time it's loaded.
- Adjusts the table view'scontentInset (as of iOS 7).
- Scrolls the table view as needed when the keyboard appears.
那么,什么时候该使用UIViewController呢?
- You need the table view to be inside another view.
- You need to add additional views to the view controller that don't scroll with the table view (though there are ways to solve this with UITableViewController).