Difference between UITableViewDelegate and UITable

2019-01-14 05:00发布

问题:

Anyone tell me the difference between UITableViewDelegate and UITableViewDatasource?

回答1:

UITableViewDelegate

Serving as a table's delegate means you provide answers to requests about the layout of the table and about actions the user performs on the tableview. Layout methods include the tableview asking about the height of rows, headers, and footers, what the buttons should look like, etc. Action methods include the user selecting a row and beginning and ending the editing of a row.
   

UITableViewDatasource

Serving as a table's datasource means you provide data for the sections and rows of a table and you act on messages that change a table's data. The datasource is asked for the data for a cell when the table is drawn, is told that the user has asked to delete a row, and is told the new value of a row that the user has edited.



回答2:

Complementing the answers, this is a sheet of all methods in delegate and datasource.

see the original answer: delegate and datasource methods for UITableView



回答3:

UITableViewDelegate is a protocol you implement on your class to receive UITableView events. UITableViewDataSource is a protocol you implement to provide data for your UITableView.

So, with UITableViewDelagate protocol you will be able to response to events like: selection on your rows, editing and several others.

With UITableViewDataSource protocol you will be able to feed data into your table. Messages defined on this protocol will be called automatically by the UITableView class.



回答4:

The data source adopts the UITableViewDataSource protocol and the delegate adopts the UITableViewDelegate protocol. UITableViewDataSource has one optional method through which the data source tells the table view how many sections it has (the default is one); it also has a required method through which it tells the table view how many rows are in each section. The UITableViewDelegate protocol declares a method that the delegate must implement to return a cell object that the table view uses when drawing a particular row. (“Table View Cells” gives more detail about this delegate task.) It also allows the delegate to modify visible aspects of the table view. Both protocols also include optional methods for responding to selections and taps on accessory views and for managing the insertion, deletion, and reordering of cells.

Learn basic at first! - TableView Guide