tableview - Table View: right to left swipe to delete does not show up - swift 3 -
i've built simple todolist app swift 3. want able delete items tableview
swiping right left. code i've found. nothing happens when swipe left.
code:
func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int{ return todolist.count } func tableview(_ tableview: uitableview, caneditrowat indexpath: indexpath) -> bool { return true } func tableview(_ tableview: uitableview, cellforrowatindexpath indexpath: indexpath) -> uitableviewcell { let cell = uitableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cell") cell.textlabel?.text = todolist[indexpath.row] return cell } // func tableview(_ tableview: uitableview, commit editingstyle: uitableviewcelleditingstyle, forrowat indexpath: indexpath) { if (editingstyle == .delete) { todolist.remove(at: indexpath.row) userdefaults.standard.set(todolist, forkey: "todolist") tableview.reloaddata() } } func tableview(_ tableview: uitableview, editingstyleforrowat indexpath: indexpath) -> uitableviewcelleditingstyle { return .delete }
this still not work. nothing happens when swipe left. list working. can add items table can't remove them.
thanks :)
did implement tableview:caneditrowatindexpath: method?
p.s: swift 3.
func tableview(_ tableview: uitableview, caneditrowat indexpath: indexpath) -> bool { return true }
edit:
thanks @rmaddy mintioning default value of tableview:caneditrowatindexpath:
true
, implementing doesn't solve problem.
i'm not pretty sure of trying code snippet, make sure implementing following methods (uitableviewdelegate):
func tableview(_ tableview: uitableview, commit editingstyle: uitableviewcelleditingstyle, forrowat indexpath: indexpath) { if (editingstyle == .delete) { todolist.remove(at: indexpath.row) userdefaults.standard.set(todolist, forkey: "todolist") tableview.reloaddata() } } func tableview(_ tableview: uitableview, editingstyleforrowat indexpath: indexpath) -> uitableviewcelleditingstyle { return .delete }
you can keep implementation of tableview:caneditrowatindexpath:
method:
asks data source verify given row editable.
so, -for example- if want let first row not editable, i.e user cannot swipe , delete first row, should somthing like:
func tableview(_ tableview: uitableview, caneditrowat indexpath: indexpath) -> bool { if indexpath.row == 0 { return false } return true }
also, make sure uitableviewdatasource
, uitableviewdelegate
connected viewcontroller.
hope helped.
Comments
Post a Comment