慕课网上学习了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程序猿的路过点赞!!

更多相关文章

  1. 【 Android(安卓)】RecyclerView 使用方法总结
  2. Android最佳性能实践(三)——高性能编码优化
  3. android 如何使用SAX解析XML
  4. 深入Android的消息机制源码详解~Handler,MessageQueue与Looper关
  5. Android热修复框架AndFix核心代码分析并改进
  6. Android中利用Handler实现消息的分发机制(三)
  7. Android应用盈利广告平台的嵌入方法详解
  8. Android(安卓)RxJava:2.0 相对于 1.0的更新 & 变化(含 RxJava 1.0
  9. android 设备调试

随机推荐

  1. 你百度不到的android坑 imageview.setima
  2. Android(安卓)Wifi P2P 入门
  3. cocos2dx调用android的说明很多,我直接给
  4. android开发,你还在犹豫什么呢?进来看看花
  5. android 自定义Dialog背景透明及显示位置
  6. Android访问WCF服务
  7. android 电量
  8. Android(安卓)读写文件的N种写法
  9. 原文:Android(安卓)Theme XML
  10. 聊聊 Android(安卓)的网络请求框架 Retro