码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-08-18 21:30:32      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:ui   android   ios   移动   结构   

慕课网上学习了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 UIKit

var 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程序猿的路过点赞!!

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:ui   android   ios   移动   结构   

原文地址:http://blog.csdn.net/u013598660/article/details/47757707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!