I don't understand who is calling the methods, for example,
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
In java, to connect an event with its listener you have to add a, for example, .addActionListener (class that handles the event) to the object. But in swift, in iOS, I don't understand who is calling these methods and in what order (speaking of tableViews).
In this code I want to add an event to the tableView, so that when a scroll is made, the method code is executed:
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
}
but I can't understand how to make the tableview listen...
You have been answered previously in a very correct way, but I am going to explain the topic of why you should carry out the inheritance of the class
UITableViewDelegate
The Delegate design pattern is a technique in which an object delegates responsibility for implementing a specific behavior to an associated object in a reverse responsibility relationship.
Indeed, by implementing the protocol
UITableViewDelegate
, you indicate that your ViewController is the delegate of those methods, so UITableView will delegate the responsibility of executing its behaviors to your ViewController class, working in a similar way to the Listener that you indicate. The methods will be called when the classUITableView
tells its delegates (in your case, the ViewController class) to execute them (similar -but not the same- to the Observer pattern)The Delegate pattern is widely used in Swift and Objective-C, so I recommend you read its structure, implementation, behavior and usability :)
Here is a recommended tutorial
Your class implements the protocols
UITableViewDelegate
andUITableViewDataSource
, therefore, it is these protocols that are in charge of calling the necessary methods to initialize your TableView.The method you mention
tableView(_:numberOfRowsInSection:)
belongs to the protocolUITableViewDataSource
, take a look at the documentation of these protocols. UITableViewDelegate and UITableViewDataSource .If you are a bit lost with all this topic of tables, you can take a look at apple's tutorial to start developing applications in iOS .
There is a protocol method
UIScrollViewDelegate
that is executed when the tableView is about to start scrolling:For those methods to be called, you must indicate who will be the "listening" delegate. For example, in the class where you have your
UITableView
you should do it like this:In this way, the methods will already be executed, which are called by the
UITableView
You still need to add the table to the view, in the viewWillAppear() for example and add what mhergon tells you with the delegate. As soon as that controller is executed, the table will appear with its 4 elements. When the controller sees that it has a table and that it implements the protocols, it takes care of executing them. If you put breakpoints in the method of number of sections and elements for each section you will see that it calls them several times, it is normal, that it does what it wants, as long as they are well implemented the table will appear.
You have to say who the delegate is, you can do this in two ways:
tableView.delegate = self
andtableView.dataSource = self