In the previous session, the value of two controllers was achieved, and the final effect
This is our homepage:
In Viewcontroller, the contents of our home page are placed in a different array:
var restaurantnames = ["Cg1", "CG2", "Cg3", "Cg4", "Cg5", "Cg6", "Cg7", "Cg8", "Cg9", "Cg10", "cg11"] var Restaurantimages = ["128.png", "129.png", "130.png", "131.png", "132.png", "133.png", "134.png", "135.png", "136". PNG "," 137.png "," 138.png "," 139.png "," 140.png "]
Today we want to integrate the information in the main page and reflect it into the jump page, which is applied to the model in the program. By observing, each of us shows the same content, the format is the same, the picture has a title, and now we separate the model. Create a new data model, a cocoa touch class, named rest, with the following code:
Import Uikitclass rest:nsobject { var name:string = "" var image:string = "" var location:string = "" var type:string = "" var Isvisit:bool = False init (NAME:STRING,IMAGE:STRING,LOCATION:STRING,TYPE:STRING,ISVI Sit:bool) { self.name = name self.image = Image self.location = location Self.type = Type Self.isvisit = Isvisit }}
Rest is the structure of our information display, now create a new class DataArray, put the initialized information into it, the code is as follows:
Import Uikitclass Dataarray:nsobject {var temparray = [Rest] ()//Temp variable var dataarray:[rest] {get { Return Temparray}} override init () {temparray = [Rest (name: "CG1", Image: "128.png", loc ation: "Xd1", type: "Cafe", Isvisit:false), Rest (name: "CG2", Image: "129.png", Location: "Xd2", type: "Cafe", Isvisi T:false), Rest (name: "Cg3", Image: "130.png", Location: "Xd3", type: "Tea", Isvisit:false), Rest (name: "CG4", IM Age: ' 131.png ', Location: ' Xd4 ', type: "Cafe", Isvisit:false), Rest (name: "Cg5", Image: "132.png", Location: "Xd5", t ype: "Tea", Isvisit:false), Rest (name: "Cg6", Image: "133.png", Location: "Xd6", type: "Cafe", Isvisit:false), R EST (name: "Cg7", Image: "134.png", Location: "Xd7", type: "Cafe", Isvisit:false), Rest (name: "Cg8", Image: "135.png", Location: "Xd8", type: "Café", Isvisit:false), Rest (name: "Cg9", Image: "136.png", Location: "Xd9", type: "Café", is Visit:false), Rest (Name: "Cg10", Image: "137.png", Location: "Xd10", type: "Cafe", Isvisit:false), Rest (name: "Cg11", Image: "138.png", Location: "Xd11", type: "Tea", Isvisit:false),]}
The relevant information in Viewcontroller is useless, we can delete it, the modified Viewcontroller code is as follows:
Import Uikitclass Viewcontroller:uiviewcontroller,uitableviewdatasource,uitableviewdelegate {var restArray = DataArr Ay (). dataarray var TableView = UITableView () override Func Viewdidload () {super.viewdidload () self.na Vigationitem.title = "Cggo" TableView = UITableView (frame:cgrectmake (0, 0, +, 568), Style:uitableviewst Yle. Plain)//define a TableView Self.view.addSubview (tableView)//Do not add to see Tableview.datasource = self tableview.d Elegate = self//was previously set in storyboard and is now manually set Tableview.registerclass (Customtableviewcell.self, Forcellreuseidentifi ER: "Cell")//Do any additional setup after loading the view, typically from a nib. } func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {return restarray.count } func Numberofsectionsintableview (Tableview:uitableview), Int {return 1; } func TableView (Tableview:uitableview, CellforrowatindexpathIndexpath:nsindexpath)-UITableViewCell {Let identistring = "cell"//code reuse var Cell = Tableview.dequeu Ereusablecellwithidentifier (Identistring,forindexpath:indexpath) as? Customtableviewcell if cell = = Nil {cell = UITableViewCell (Style:UITableViewCellStyle.Default, Reuseid entifier:identistring) as? Customtableviewcell} var rest = Restarray[indexpath.row] var restname = rest.name var restloc ation = rest.location var imageName = rest.image var resttype = Rest.type cell?. Initwith (ImageName, Restname:restname, Restlocation:restlocation, Resttype:resttype) if rest.isvisit{ Cell?. Accessorytype =. Checkmark} else {cell?. Accessorytype =. None} return cell! } override func Prefersstatusbarhidden () Bool {//Hide power, signal, etc. in the top bar return true} func Tablevi EW (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexPath) {Tableview.deselectrowatindexpath (Indexpath, animated:true) Let Callactionhandler = {(action : uialertaction!) -Void in Let Alertmessage = Uialertcontroller (title: ' Service is unavalable ', message: "You can choice Another rest ", PreferredStyle:UIAlertControllerStyle.Alert) alertmessage.addaction (uialertaction (title: "OK", Style:UIAlertActionStyle.Default, Handler:nil)) Self.presentviewcontroller (Alertmessage, animated: True, Completion:nil)} Let option = Uialertcontroller (title:nil, message: "What is goning to do?" ", PreferredStyle:UIAlertControllerStyle.ActionSheet)//callaction Let callaction = uialertaction (title : "Call" + "180-123-\ (Indexpath.row)", Style:UIAlertActionStyle.Default, Handler:callactionhandler)// Custom Callactionhandler to respond to clicked events//markaction let Markaction = uialertaction (title: "I'm Here", style: Uialertactionstyle.default,Handler: {(action:uialertaction!)->void in Let cell = Tableview.cellforrowatindexpath (i Ndexpath) cell?. Accessorytype = Uitableviewcellaccessorytype.checkmark//Checkmark Self.restarray[indexpath.row].isvisit = True }) option.addaction (markaction)//cancelaction Let cancelaction = uial Ertaction (title: "OK", Style:UIAlertActionStyle.Cancel, Handler:nil)//self.presentviewcontroller (option, animate D:true, completion:nil) Let detail = Detailviewcontroller () Self.navigationcontroller? . Pushviewcontroller (detail, animated:true) var image = Restarray[indexpath.row].image Detail.imagen Ame = Image option.addaction (cancelaction) option.addaction (callaction)} func TableView (TableView : UITableView, Heightforrowatindexpath indexpath:nsindexpath), cgfloat {return) func TablevIew (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, ForRowAtIndexPath Indexpath:nsindexpath) {if Editingstyle = =. Delete {self.restArray.removeAtIndex (Indexpath.row)} self.tableView.deleteRowsAtIndexPaths ([index Path], WithRowAnimation:UITableViewRowAnimation.Left)} func TableView (Tableview:uitableview, Editactionsforrowati Ndexpath indexpath:nsindexpath) [Anyobject]? {Let shareaction = Uitableviewrowaction (style:. Default, Title: "Share", Handler: {(action:uitableviewrowaction!, indexpath:nsindexpath!), Void in Let menu = Uialertcontroller (title: "Share Action", Message:nil, Preferredstyle:. Actionsheet) Let csdnaction = uialertaction (title: "Csdn", Style:. Default, Handler:nil) menu.addaction (csdnaction) Let cancelaction = uialertaction (title: "Cancel", Style:. Cancel, Handler:nil) menu.addaction (cancelaction) Self.presentviewcontroller (menu, Animated:true, Completion:nil)}) let de Leteaction = Uitableviewrowaction (style:. Default, Title: "Delete", Handler: {(action:uitableviewrowaction!,indexpath:nsindexpath!), Void in }) return [Deleteaction, ShareAction]}}
You can see that the previous single array was replaced by the model named Restarray, referencing the Restarray instance in the proxy method that defines cel, showing the effect
Swift language IOS8 development war 9.Data Model