UITableView is a very common control in iOS that is used primarily for the presentation of list content. There are two default built-in styles: Uitableviewstyleplain and uitableviewstylegrouped. uitableviewstylegrouped allows you to group the contents of the list. Uitableviewcontroller is dedicated to UITableView View Controller, provides a series of methods to create and maintain UITableView.
Uitableviewcontroller must inherit two protocols:
1, the data is provided to the UITableView through Uitableviewdatasource, and the content is populated for each cell cells.
2, through Uitableviewdelegate to handle the selection of each cell cells (such as click to select, etc.).
Common considerations include the following:
1, create a new Uitableviewcontroller subclass, and make it inherit the Uitableviewdatasource and Uitableviewdelegate protocols.
2, create UITableView through storyboard and point its DataSource and delegate to the Uitableviewcontroller object
(Right-click the UITableView object in storyboard, select and connect).
3, for the UITableView, there must be a rewrite of at least two methods,
func TableView (Table:uitableview, Numberofrowsinsection section:int), Int {}
This method returns the number of cells rendered in tableview, such as return Self.products.count
func TableView (Table:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {
Let cell = UITableViewCell (Style:UITableViewCellStyle.SubTitle, Reuseidentifier: "MyCell")
cell.textLabel.text = self.products[indexpath.row][' title '] as? String
return cell
}
This method is used to describe each cell's style and the data it fills, and then returns that cell.
The basic style of each cell can be pre-set by identifier, such as basic, subtitle, etc., or you can set a custom type MyCell.
Let Cell:uitableviewcell = Tableview.dequeuereusablecellwithidentifier ("Basic", Forindexpath:indexpath) as UITableViewCell
This method removes a cell from the TableView queue for reuse.
4, in addition to the above two methods that must be overridden, set to select each cell corresponding to the operation, you can use the following methods,
func TableView (Tableview:uitableview, Didselectrowatindex indexpath:nsindexpath!) {Self.didchooseitem (Indexpath.row)}
This method is dedicated to the operation that is performed when the cell is selected.
func TableView (tableview:uitableview!, Willdisplaycell cell:uitableviewcell!, Forrowatindexpath indexPa th:nsindexpath!) { }
This method is the action that is performed when the cell is selected, such as displaying a warning or an animation.
5, the use of delegate.
Delegate's idea is actually very interesting, the behavior that an object cannot do, can give it to set a proxy object, and the proxy object can complete this behavior instead.
Use in UITableView is very common. For example, fetching data from the network to populate the TableView, We can encapsulate the operation of acquiring network data onrequest in a Httpcontroller class. The obtained data is bound to the Httpcontroller object. And we want to be able to pass it directly to Tableviewcontroller. Instead of passing data to Tableviewcontroller through the Httpcontroller object.
First, define a protocol in Httpcontroller,
Protocol Httpprotocol {
Func parseresults (results:nsdictionary)
}
The Tableviewcontroller class is then inherited by the Httpprotocol protocol, and the function body of the parseresults (results:nsdictionary) is defined.
Next, define a Httpcontroller object in Tableviewcontroller and specify that the delegate object of the Httpctl is the Tableviewcontroller itself.
var httpctl:httpcontroller = Httpcontroller ()
Httpctl.delegate = Self
This step corresponds to the Httpcontroller class to define a property var Delegate:httpprotocol?
Httpctl.onrequest ("www.dianping.com")
So, the process of delegate is that the Httpcontroller object Httpctl calls the ONrequest method, and the ONrequest method obtains the original value of the network data. The Parseresults method needs to be called to parse the data. The function body of parseresults is defined in Tableviewcontroller. The final process is not, self.delegate is used in httpctl.onrequest?. Parseresults (Jsonresult) can parse the network data. At the same time, the parsed data in the Parseresults function body can be passed directly to the Tableviewcontroller (the delivery process is, The properties directly assigned to the Tableviewcontroller, such as the products array.
Above, is the basic process that the delegate takes place. The description is not clear, welcome all the classmates to discuss together.
In fact, do not adopt delegate way can also achieve the above functions, will be parseresults function body directly defined as Httpcontroller own method, The acquisition of network data and the resolution of network data are done in Httpcontroller. Finally, the data is passed from Httpcontroller to Tableviewcontroller by means of segue. But it involves more complex data transfer or manipulation, It is also recommended to use the idea of delegate to achieve.
So, the basic usage of UITableView is written here, if there is inaccurate place, please correct me.
UITableView---Common control usage Summary for iOS development