After we used our own defined controller, we found that the restaurant on the TableView was gone, so we add the data, add a new restaurant class, this class we don't need to inherit the class of the system, just add a swift file.
Import Foundationclass Restaurant { var name = "" var location = "" var score = 10}
I've simply built a class that represents restaurant names, restaurant locations, and restaurant ratings, all with default values.
And then we go back to the restaurant list. Create an array of restaurants in the controller.
var restaurantlist = [Restaurant] ()
Then add a method to initialize the data.
Func Loadinitdata () {let rest1 = Restaurant () rest1.name = "Shu yu shu er" restaurantlist.append (rest1) let Rest2 = Restaurant () rest2.name = "Phuong Fong Restaurant" Restaurantlist.append (rest2) let rest3 = Restaurant () Rest3.name = "Kelly Restaurant" Restaurantlist.append (REST3) let rest4 = Restaurant () rest4.name = "Old bowl noodles in the sauce house" Restaurantlist.append (REST4) let rest5 = Restaurant () rest5.name = "to de parity Sichuan" restaurantlist.append ( REST5) Let rest6 = Restaurant () rest6.name = "Big bowl Rice" restaurantlist.append (REST6) }
Then it's OK to call it in the viewdidload. But now that we're running, we're going to find that the table is still empty. It turns out that we didn't use the delegate and we needed to delegate the updated table content to the controller. Because we are sub-agents, we need to overload the controller's many methods, these methods in our controller code has been written well we just need to hold down their own requirements to make changes, such as the following method:
Override func Numberofsectionsintableview (Tableview:uitableview), Int { //#warning potentially incomplete Method implementation. Return the number of sections. Return 1 }
This method specifies the block of the table, some of which may be more complex than one, and we return 1 for only one chunk.
Override Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int { //#warning Incomplet E method implementation. Return the number of the rows in the section. Return Restaurantlist.count }
The above method is used to specify the number of rows of the table, we return the number of elements of the array, there are many settings method name is TableView, but the parameters are different, people choose their own needs. Usually the format of each row of cells is the same, so we need to set a default format to reuse, open storyboard, select a table view cell and then set identifier, named PCell. The bottom selection is the selected style, which has the following types:
This is what color will be selected, we choose None. Back to the code:
Override Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { C1/>let cell = Tableview.dequeuereusablecellwithidentifier ("PCell", Forindexpath:indexpath) as UITableViewCell / /Configure the cell ... return cell }
The function of this method is to reuse the default format we just set. Now we're done with the cell format and the content in the above method. Add a line:
Cell.textlabel?. Text = restaurantlist[indexpath.row].name//number of rows
Operating effect:
Swift UI Special Training 7 data addition