Swift UI special training 8 Show data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.