ios - Swift, add swipe to delete UITableViewCell -


i learning swift ios , making checklist application uitableview. wondering how add swipe delete uitableviewcell.

this viewcontroller.swift:

import uikit  class viewcontroller: uiviewcontroller,  uitextfielddelegate, uitableviewdelegate,      uitableviewdatasource {  var tableview: uitableview! var textfield: uitextfield! var tableviewdata:array<string> = []  // define colors  let lightcolor: uicolor = uicolor(red: 0.996, green: 0.467, blue: 0.224, alpha: 1) let medcolor: uicolor = uicolor(red: 0.973, green: 0.388, blue: 0.173, alpha: 1) let darkcolor: uicolor = uicolor(red: 0.800, green: 0.263, blue: 0.106, alpha: 1) let greencolor: uicolor = uicolor(red: 0.251, green: 0.831, blue: 0.494, alpha: 1)  init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?) {     super.init(nibname: nibnameornil, bundle: nibbundleornil)     // custom initialization }  override func viewdidload() {     super.viewdidload()      //set table view      self.tableview = uitableview(frame: cgrectmake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100), style: uitableviewstyle.plain)     self.tableview.registerclass(mytableviewcell.self, forcellreuseidentifier: "mycell")     self.tableview.backgroundcolor = darkcolor     //self.tableview.separatorstyle = uitableviewcellseparatorstyle.none     self.tableview.delegate = self     self.tableview.datasource = self      self.view.addsubview(self.tableview)      //set text field      self.textfield = uitextfield(frame: cgrectmake(0, 0, self.view.bounds.size.width, 100))     self.textfield.backgroundcolor = lightcolor     self.textfield.font = uifont(name: "avenirnext-bold", size: 26)     self.textfield.delegate = self      self.view.addsubview(self.textfield)    }  //table view delegate  func tableview(tableview: uitableview!, numberofrowsinsection section: int) -> int {      return tableviewdata.count  }  func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell! {      var mynewcell: mytableviewcell = tableview.dequeuereusablecellwithidentifier("mycell", forindexpath: indexpath) mytableviewcell     mynewcell.text = self.tableviewdata[indexpath.row]      return mynewcell  }  func tableview(tableview: uitableview!, didselectrowatindexpath indexpath: nsindexpath!) {      let myselectedcell:uitableviewcell = tableview.cellforrowatindexpath(indexpath)      //colors      myselectedcell.detailtextlabel.textcolor = uicolor.whitecolor()     myselectedcell.tintcolor = uicolor.whitecolor()      //setup details / date      let mydate:nsdate = nsdate()     var mydateformatter:nsdateformatter = nsdateformatter()     mydateformatter.datestyle = nsdateformatterstyle.mediumstyle      myselectedcell.detailtextlabel.text = mydateformatter.stringfromdate(mydate)     myselectedcell.accessorytype = uitableviewcellaccessorytype.checkmark     myselectedcell.backgroundcolor = greencolor  }  override func prefersstatusbarhidden() -> bool {     return true  }  //text field delegate  func textfieldshouldreturn(textfield: uitextfield!) -> bool {      tableviewdata.append(textfield.text)     textfield.text = ""     self.tableview.reloaddata()     textfield.resignfirstresponder()     return true  }  } 

and mytableviewcell.swift:

import uikit  class mytableviewcell: uitableviewcell {  let medcolor: uicolor = uicolor(red: 0.973, green: 0.388, blue: 0.173, alpha: 1)  init(style: uitableviewcellstyle, reuseidentifier: string) {     super.init(style: uitableviewcellstyle.subtitle, reuseidentifier: reuseidentifier)      self.textcolor = uicolor.whitecolor()     self.backgroundcolor = medcolor     self.selectionstyle = uitableviewcellselectionstyle.none }  override func awakefromnib() {     super.awakefromnib()     // initialization code }  override func setselected(selected: bool, animated: bool) {     super.setselected(selected, animated: animated)      // configure view selected state }  } 

i using ios8 deployment target (not sure of difference make).

add these 2 functions:

func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool {     return true }  func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {     if (editingstyle == uitableviewcelleditingstyle.delete) {         // handle delete (by removing data array , updating tableview)     } } 

swift 3.0:

func tableview(_ tableview: uitableview, caneditrowat indexpath: indexpath) -> bool {     return true }  func tableview(_ tableview: uitableview, commit editingstyle: uitableviewcelleditingstyle, forrowat indexpath: indexpath) {     if (editingstyle == uitableviewcelleditingstyle.delete) {         // handle delete (by removing data array , updating tableview)     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -