iOS Tableview侧滑删除和移动cell的实现

慕课网上学习了tableview的使用,,突然让我觉得iOS比android简单多了,可能是我的感觉吧。因为android实现list view侧拉删除,动态移动item过程还是稍微有点复杂的。但是iOS却只需要重写几个方法就可以实现了。我只能说iOS太神奇!我就跟着做了一下。 项目地址:Todo

看效果,UI还可以。先上storyboard结构图:

navigate controller 实现一个导航栏。view controller 实现一个tableview,tableviewCell 。

import UIKitvar todos:[TodoModel] = []var filtertodos:[TodoModel] = []func dateFromString(dateStr:String) -> NSDate?{let dateFormatter = NSDateFormatter()dateFormatter.dateFormat = “yyyy-MM-dd”let date = dateFormatter.dateFromString(dateStr)return date}class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate{@IBOutlet weak var tableView: UITableView!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.todos = [TodoModel(id: “1”, image: “child-selected”, title: “1. 去游乐场”, date: dateFromString(“2014-10-20”)!),TodoModel(id: “2”, image: “shopping-cart-selected”, title: “2. 购物”, date: dateFromString(“2014-10-28”)!),TodoModel(id: “3”, image: “phone-selected”, title: “3. 打电话”, date: dateFromString(“2014-10-30”)!),TodoModel(id: “4”, image: “travel-selected”, title: “4. Travel to Europe”, date: dateFromString(“2014-10-31”)!)]navigationItem.leftBarButtonItem = editButtonItem()var contentOffset = tableView.contentOffsetcontentOffsettableView.contentOffset = contentOffset}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}//返回加载的条数 方法来自UITableViewdelegatefunc tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{if tableView == searchDisplayController?.searchResultsTableView {return filtertodos.count}else{return todos.count}}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{let cell = (“todocell”) as! UITableViewCellvar todo:TodoModel?if tableView == searchDisplayController?.searchResultsTableView {todo = filtertodos[indexPath.row] as TodoModel}else{todo = todos[indexPath.row] as TodoModel}var title = cell.viewWithTag(103) as! UILabelvar date = cell.viewWithTag(104) as! UILabelvar image = cell.viewWithTag(102) as! UIImageViewimage.image = UIImage(named:todo!.image)title.text = todo!.titlelet locale = NSLocale.currentLocale()let dateFormat = NSDateFormatter.dateFormatFromTemplate(“yyyy-MM-dd”, options: 0, locale: locale)let dateFormatter = NSDateFormatter()dateFormatter.dateFormat = dateFormatdate.text = dateFormatter.stringFromDate(todo!.date)return cell}//mark-UITableViewdelegatefunc tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){if editingStyle == UITableViewCellEditingStyle.Delete{todos.removeAtIndex(indexPath.row)([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)}}override func setEditing(editing: Bool, animated: Bool) {super.setEditing(editing, animated: animated)(editing, animated: animated)}//点击确定按钮 unsegue 操作 @IBAction func close(segue:UIStoryboardSegue){println(“close”)tableView.reloadData()}//实现侧滑删除,方法来自 UITableViewDelegatefunc tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{return editing} //实现cell移动,方法来自 UITableViewDelegatefunc tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath){let todo = todos.removeAtIndex(sourceIndexPath.row)todos.insert(todo, atIndex: destinationIndexPath.row)}}

实现tableview 很明显就是给一个Datasoure和一个delegate。view controller extension UITableViewDataSource(处理数据源),UITableViewDelegate (处理tableview各种操作)

最后记得绑定datasoure和delegate 。貌似是不是很简单。我靠,android程序猿的路过点赞!!

没有创造的生活不能算生活,只能算活着。

iOS Tableview侧滑删除和移动cell的实现

相关文章:

你感兴趣的文章:

标签云: