iOS Tableview侧滑删除和移动cell的实现
16lz
2021-01-25
慕课网上学习了tableview的使用,突然让我觉得iOS比android简单多了,可能是我的感觉吧。因为android实现list view侧拉删除,动态移动item过程还是稍微有点复杂的。但是iOS却只需要重写几个方法就可以实现了。我只能说iOS太神奇!我就跟着做了一下。
项目地址:Todo
看效果,UI还可以。先上storyboard结构图:
navigate controller 实现一个导航栏。view controller 实现一个tableview,tableviewCell 。
//// ViewController.swift// Todo//// Created by zhouyihua on 15/8/17.// Copyright (c) 2015年 xiebangyuan. All rights reserved.//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.contentOffset contentOffset.y += searchDisplayController!.searchBar.frame.size.height tableView.contentOffset = contentOffset } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //返回加载的条数 方法来自UITableViewdelegate func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ if tableView == searchDisplayController?.searchResultsTableView { return filtertodos.count }else{ return todos.count } } // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) //返回tableview的cell 就是list view的 item func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = self.tableView.dequeueReusableCellWithIdentifier("todocell") as! UITableViewCell var 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! UILabel var date = cell.viewWithTag(104) as! UILabel var image = cell.viewWithTag(102) as! UIImageView image.image = UIImage(named:todo!.image) title.text = todo!.title let locale = NSLocale.currentLocale() let dateFormat = NSDateFormatter.dateFormatFromTemplate("yyyy-MM-dd", options: 0, locale: locale) let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = dateFormat date.text = dateFormatter.stringFromDate(todo!.date) return cell } //mark-UITableViewdelegate func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){ if editingStyle == UITableViewCellEditingStyle.Delete{ todos.removeAtIndex(indexPath.row) self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic) } } override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) self.tableView.setEditing(editing, animated: animated) } //点击确定按钮 unsegue 操作 @IBAction func close(segue:UIStoryboardSegue){ println("close") tableView.reloadData() } //实现侧滑删除,方法来自 UITableViewDelegate func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{ return editing } //实现cell移动,方法来自 UITableViewDelegate func 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程序猿的路过点赞!!
更多相关文章
- 【 Android(安卓)】RecyclerView 使用方法总结
- Android最佳性能实践(三)——高性能编码优化
- android 如何使用SAX解析XML
- 深入Android的消息机制源码详解~Handler,MessageQueue与Looper关
- Android热修复框架AndFix核心代码分析并改进
- Android中利用Handler实现消息的分发机制(三)
- Android应用盈利广告平台的嵌入方法详解
- Android(安卓)RxJava:2.0 相对于 1.0的更新 & 变化(含 RxJava 1.0
- android 设备调试