Now I want to click on the entry in the form, mark it, click it again to cancel it, so how do I do it? is still using the overloaded method of the TableView,
A new iscollected value in restaurant indicates whether to collect, and then goes back to Restaurantlistviewcontroller, adding:
Override Func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {let r1 = restaurantlist[indexpath.row]//take out the clicked line r1.iscollected =!r1.iscollected//Click to reverse the collection state Tableview.deselectrowatindexpath (Indexpath, Animated:false) tableview.reloaddata () }
But when the operation is not reactive, although the state has been changed, but not reflected on the page, it should now add a tag on the page, the changed control cell TableView method is as follows:
Override Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { C1/>let cell = Tableview.dequeuereusablecellwithidentifier ("PCell", Forindexpath:indexpath) as UITableViewCell Let R1 = Restaurantlist[indexpath.row] //Configure the cell ... Cell.textlabel?. Text = r1.name//number of rows if r1.iscollected{ cell.accessorytype = Uitableviewcellaccessorytype.checkmark } else { Cell.accessorytype = Uitableviewcellaccessorytype.none } return cell }
To run the effect, click on the entry:
Click on the cancellation, in addition to the hook there are many interesting signs, you can try.
There are also edit buttons on the navigation, and now let's implement the editing function. Now clicking on the Edit button on the left is not responding, we need to add the following statement in Viewdidload:
Self.navigationItem.leftBarButtonItem = Self.editbuttonitem ()
To let the system know the navigation coordinates of the button is our Edit button, and then add a new method in the controller setediting, this is also auto-complete, the code is as follows:
Override Func setediting (Editing:bool, Animated:bool) { super.setediting (editing, animated:true)//Implements the parent class first Tableview.setediting (editing, animated:true) }
Now we click the Edit button, the effect is as follows:
Clicking Finish will return. Click on the left side of the red icon will slide out of the Delete button, click the button will delete the current line, just need to add a new method in the controller, the code is as follows:
Override Func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexpath:nsindexpath) {if Editingstyle = = uitableviewcelleditingstyle.delete{//If the delete button Restaurantlist.removeatindex (Indexpath.row)//First delete the elements in the array tableview.deleterowsatindexpaths ([Indexpath], WITHROWANIMATION:UITABLEVIEWROWANIMATION.TOP)//delete list line, other line push up } }
We can try the effect.
Swift UI special training 8 Show data