allenylzhou
allenylzhou

Reputation: 1461

What am I overriding in UITableViewController?

A lot of tutorials on UITableView asks us to override this function in MyUITableViewController: UITableViewController:

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    return 10
}

I understand this is an implementation of the numberOfRowsInSection method in UITableView but I don't understand the language mechanism.

tableView is an inherited property of UITableViewController, not a method. So how exactly does the override work?

Upvotes: 3

Views: 530

Answers (2)

allenylzhou
allenylzhou

Reputation: 1461

UITableViewController conforms to the UITableViewDataSource protocol: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html

tableView is a protocol function defined in UITableViewDataSource

UITableViewController has a default implementation of protocol functions which MyUITableViewController can override.

Upvotes: 1

Paulw11
Paulw11

Reputation: 114975

The objective-C syntax of that same method is

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section; 

which gives a method signature of tableView:numberOfRowsInSection:

When you are used to Objective-C you kind of mentally ignore the tableView: part and focus on the numberOfRowsInSection.

When the Objective-C method name is converted to swift, the first parameter becomes the method name so you get tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int

The short answer to your question is that you are overriding the function with that signature, not the tableView property

Upvotes: 3

Related Questions