あなたを助けるために、UIViewControllerはあなたがサブクラス化しているクラスです。これはプロトコルではないため、実装に必要なメソッドはありません。ただし、継承したメソッドはオーバーライドできます。これは、最初にファイルを作成したときに表示されるものです。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
これは、UIViewController から継承したメソッドをオーバーライドします。呼び出し以外に何もしていない場合はsuper.viewDidLoad()
、コードから関数全体を削除できます。
現在、UITableViewDataSource および UITableViewDelegate または両方のプロトコルであるため、実装に必要なメソッドがある場合とない場合があります。
Alt キーを押しながらクリックしたときに説明する内容は、UITableViewDataSource を右クリックして定義へのジャンプを選択した場合と同じです。これは、実際の Mac でコマンド クリックすると得られるものです。チュートリアルが言っていることは、必要なメソッドが一番上にあるということです。これは、ドキュメントを見ると常にそうであるとは限りません (メソッドは目的ごとに整理されているため)。UITableViewDataSource の場合、次のように表示されます。
protocol UITableViewDataSource : NSObjectProtocol {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
optional func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
optional func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
optional func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
最初の 2 つのメソッドfunc tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
とfunc tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
の前にオプションがないことに気付くでしょう。これは、これらがこのプロトコルに必要なメソッドであることを意味します。以下のオプションはオプションです。
UITableViewDelegate の場合、オプションではないメソッドがないことがわかります。したがって、実装する必要はありません。