iOS development--ui_swift &uitableview implement moving cells

Source: Internet
Author: User

UITableView Implementing moving cells
1, the following example is to add cell movement function to table UITableView:
    • (1) Add long press function to the table, long press the form to enter the editing state
    • (2) In edit state, you can see the drag button appears after the cell
    • (3) Press and hold the drag button, you can drag the cell to any location
    • (4) The Tabelview corresponding agent event is also triggered when the drag is complete.
2, as follows: 3, the code is as follows
1 Import UIKit2  3 classViewcontroller:uiviewcontroller,uitableviewdelegate,4 Uitableviewdatasource,uigesturerecognizerdelegate {5      6var tableview:uitableview?7      8var ctrlnames:[string] = ["UILabel Label","UIButton Button","Uidatepiker Date Selector",9         "UITableView Table View"]Ten       One     Overridefunc viewdidload () { A super.viewdidload () -           -         //Create a table view theSelf.tableview =UITableView (Frame:UIScreen.mainScreen (). Applicationframe, - Style:UITableViewStyle.Plain) -self.tableview!.Delegate= Self -self.tableview!. DataSource = Self +         //to create a reusable cell -self.tableview!. RegisterClass (Uitableviewcell.self, Forcellreuseidentifier:"Swiftcell") +Self.view.addSubview (self.tableview!) A           at         //binding a response to a long press -var longpress =Uilongpressgesturerecognizer (target:self, -Action:selector ("tableviewcelllongpressed:")) -         //Agent -Longpress.Delegate= Self -Longpress.minimumpressduration =1.0 in         //add a long-tap gesture to a view that needs to implement a long-press operation -self.tableview!. Addgesturerecognizer (longpress) to     } +       -     //in this case, there is only one partition theFunc Numberofsectionsintableview (tableview:uitableview!)Int { *         return 1; $     }Panax Notoginseng       -     //returns the number of table rows (that is, the number of returned controls) theFunc TableView (Tableview:uitableview, numberofrowsinsection section:int)Int { +         returnSelf.ctrlnames.count A     } the       +     //Create individual cell display content (create the cell specified by the parameter Indexpath) - func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath) $-UITableViewCell $     { -         //to provide tabular display performance, the units that have been created must be reused -Let identify:string ="Swiftcell" the         //duplicate cells in the same form, registered at the time of declaration -Let cell =Tableview.dequeuereusablecellwithidentifier (Identify, Forindexpath:indexpath)Wuyi              asUITableViewCell theCell.accessorytype =Uitableviewcellaccessorytype.disclosureindicator -Cell.textlabel?. Text =Self.ctrlnames[indexpath.row] Wu         returnCell -     } About       $     //Long Press the table - func tableviewcelllongpressed (gesturerecognizer:uilongpressgesturerecognizer) -     { -         if(Gesturerecognizer.state = =uigesturerecognizerstate.ended) A         { +println"uigesturerecognizerstateended"); the             //toggle between Normal and edit states -             if(self.tableview!. editing = =false){ $self.tableview!. Setediting (true, Animated:true) the             } the             Else{ theself.tableview!. Setediting (false, Animated:true) the             } -         } in     } the       the     //in edit state, you can drag to set the cell location AboutFunc TableView (Tableview:uitableview, Canmoverowatindexpath Indexpath:nsindexpath)Bool { the         return true the     } the       +     //Move Cell Event - func TableView (Tableview:uitableview, Moverowatindexpath Fromindexpath:nsindexpath, the Toindexpath:nsindexpath) {Bayi         ifFromindexpath! =toindexpath{ the             //get values for moving rows thevar itemvalue:string =Ctrlnames[fromindexpath.row] -             //Delete a moved value - Ctrlnames.removeatindex (Fromindexpath.row) the             //If the moving area is greater than the number of existing rows, add the moved value directly at the end the             ifToindexpath.row >ctrlnames.count{ the ctrlnames.append (Itemvalue) the}Else{ -                 //no more than the maximum number of rows, the value you just deleted is added at the target location the Ctrlnames.insert (Itemvalue, AtIndex:toIndexPath.row) the             } the         }94     } the}

iOS development--ui_swift &uitableview implement moving cells

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.