"Fine" form (UITableView) Summary (4): Edit (Add, delete, move)

Source: Internet
Author: User

Reprint please declare source: http://blog.csdn.net/jinnchang/article/details/45934781

1. Preface

The move (order) implements the following methods:

Set which rows can be moved by Func TableView (Tableview:uitableview, Canmoverowatindexpath indexpath:nsindexpath), bool// Set move operation Func TableView (Tableview:uitableview, Moverowatindexpath Sourceindexpath:nsindexpath, Toindexpath Destinationindexpath:nsindexpath)
Editing (delete, add) implements the following methods:
Set which lines can be edited by Func TableView (Tableview:uitableview, Caneditrowatindexpath indexpath:nsindexpath), Bool    // Sets the button style that appears to the left of each line in edit mode func tableView (Tableview:uitableview, Editingstyleforrowatindexpath Indexpath:nsindexpath) Uitableviewcelleditingstyle    //Set edit operation (Delete, insert) func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath Indexpath:nsindexpath)    //Set slide operation display typeface func TableView (Tableview:uitableview, Titlefordeleteconfirmationbuttonforrowatindexpath indexpath:nsindexpath), String!
2. Demo Example


3. Demo Code

viewcontroller.swift//uitableviewsample-editor////Created by Jinnchang on 15/5/21.//Copyright (c) 2015 Jinn C Hang.        All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller, uitableviewdelegate, UITableViewDataSource {        var tableview:uitableview!    var leftbutton:uibarbuttonitem!        var rightbutton:uibarbuttonitem!        var data:nsmutablearray! Override Func Viewdidload () {super.viewdidload ()//Do any additional setup after loading the view, typical                Ly from a nib. Self.title = "singer List" data = ["Leehom Wang", "May Days", "Jay Chou"] LeftButton = Uibarbuttonitem (title: "New", sty Le:UIBarButtonItemStyle.Plain, target:self, Action: "Addaction") Self.navigationItem.leftBarButtonItem = Leftbutt On Rightbutton = Uibarbuttonitem (title: "Edit", Style:UIBarButtonItemStyle.Plain, target:self, action: "EDI Taction ") Self.navigationItem.rightBarButtonItem = RigHtbutton TableView = UITableView (frame:self.view.bounds, Style:UITableViewStyle.Plain) TABLEVIEW.A                Llowsselectionduringediting = TRUE//edit allows the row to be selected tableview.delegate = self Tableview.datasource = self Self.view.addSubview (TableView)} override func didreceivememorywarning () {Super.didreceivememoryw    Arning ()//Dispose of any of the resources that can is recreated. }//Set the number of rows per segment func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {retur N Data.count}//Set specific contents of each line func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpa TH)-UITableViewCell {var cell = Tableview.dequeuereusablecellwithidentifier ("cell") as? UITableViewCell if (cell = = nil) {cell = UITableViewCell (Style:UITableViewCellStyle.Default, reuseident Ifier: "Cell")} cell!. Textlabel?. Text = Data.objectatindex (Indexpath.row) as? String return cell! }//Selected row operation Func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {if (Ta bleview.editing) {Let cell = Tableview.cellforrowatindexpath (indexpath) let param = cell!. Textlabel?. Text println (param)} tableview.deselectrowatindexpath (Indexpath, Animated:true)}// Sets which rows can be moved by Func TableView (Tableview:uitableview, Canmoverowatindexpath indexpath:nsindexpath), Bool {ret Urn true}//Set move operation Func TableView (Tableview:uitableview, Moverowatindexpath Sourceindexpath:nsindexpath, Toindexpath destinationindexpath:nsindexpath) {Let Fromrow = sourceindexpath.row let Torow = Destinationin        Dexpath.row var obj:anyobject = Data.objectatindex (fromrow) data.removeobjectatindex (Fromrow) Data.insertobject (obj, Atindex:torow)}//set which rows can be edited by the Func TableView (Tableview:uitableview, CaneditrOwatindexpath Indexpath:nsindexpath), Bool {return true}//Set the button style shown to the left of each row in edit mode func TableView ( Tableview:uitableview, Editingstyleforrowatindexpath Indexpath:nsindexpath), Uitableviewcelleditingstyle {R Eturn Uitableviewcelleditingstyle.delete}//Set edit operation (Delete, insert) func TableView (Tableview:uitableview, Commitedit Ingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexpath:nsindexpath) {if (EditingStyle = = Uitableviewcelleditingstyle.delete) {data.removeobjectatindex (Indexpath.row) Tableview.deleterowsat Indexpaths ([Indexpath], WithRowAnimation:UITableViewRowAnimation.Fade)}}//Set slide operation to display the typeface func Tablevi EW (Tableview:uitableview, Titlefordeleteconfirmationbuttonforrowatindexpath indexpath:nsindexpath), String! {return ' delete '}//' edit ' button responds to event func editaction () {if (Editingstate ()) {Rightbutton. title = "Edit" TableviEw.setediting (False, Animated:true)} else {rightbutton.title = "done" tableview.setediting (t Rue, Animated:true)}}///Determines whether currently in the Edit state func editingstate (), Bool {return rightbutton.t        Itle = = "Done"}//"Add" button response event func Addaction () {Data.insertobject ("mystic Singer", AtIndex:data.count) var Indexpath = Nsindexpath (forrow:data.count-1, insection:0) tableview.insertrowsatindexpaths ([IndexPath], WI ThRowAnimation:UITableViewRowAnimation.Bottom)}}
Project on GitHub address: Https://github.com/jinnchang/SwiftSamples/blob/master/UITableViewSample-Editor4. Conclusion

Article last updated: May 23, 2015 15:59:57

"Fine" form (UITableView) Summary (4): Edit (Add, delete, move)

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.