Implementation of iOS Tableview slide deletion and mobile cell

Source: Internet
Author: User
Tags dateformat

Implementation of iOS Tableview slide deletion and mobile cell

MOOC learned how to use tableview on the Internet and suddenly made me feel that iOS is much simpler than android. Maybe it is my feeling. The process of dynamically moving items is a little complicated because android implements list view-side deletion. However, iOS only needs to rewrite several methods. I can only say that iOS is amazing! I followed it.
Project address: Todo

The UI can also be used to view the results. First, run the injection? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnlib2FyZL3hubnNvKO6PGJyIC8 + DQo8aW1nIGFsdD0 =" here write picture description "src =" http://www.bkjia.com/uploads/allimg/150829/12554R956-4.png "title =" \ "/>
Navigate controller implements a navigation bar. View controller implements a tableview and 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. go to the playground, date: dateFromString )!), TodoModel (id: 2, image: shopping-cart-selected, title: 2. shopping, date: dateFromString (2014-10-28 )!), TodoModel (id: 3, image: phone-selected, title: 3. Call, date: dateFromString )!), TodoModel (id: 4, image: travel-selected, title: 4. Travel to Europe, date: dateFromString ()!)] NavigationItem. leftBarButtonItem = editButtonItem () var contentOffset = tableView. contentOffset. y + = searchDisplayController !. SearchBar. frame. size. height tableView. contentOffset = contentOffset} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} // The method for returning the number of loaded entries is from UITableViewdelegate func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {if tableView = searchDisplayController ?. SearchResultsTableView {return filtertodos. count} else {return todos. count }}// Row display. implementers shocould * always * try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with outputs: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) // return ta The cell of bleview is the item func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) of list view-> UITableViewCell {let cell = self. tableView. Destroy (todocell)! 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)! UILabel var date = cell. viewWithTag (104)! UILabel var image = cell. viewWithTag (102)! 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 = response. 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)} // click the "OK" button to unsegue the operation @ IBAction func close (segue: UIStoryboardSegue) {println (close) tableView. reloadData ()} // implement slide deletion. The method is from UITableViewDelegate func tableView (tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath)-> Bool {return editing} // implement cell movement, the method is from UITableViewDelegate func tableView (tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {let todo = todos. removeAtIndex (sourceIndexPath. row) todos. insert (todo, atIndex: destinationIndexPath. row )}}

The implementation of tableview is obviously to give a Datasoure and a delegate. View controller extension UITableViewDataSource (processing data sources) and UITableViewDelegate (processing various tableview operations)

Finally, remember to bind datasoure and delegate. Is it easy. Thanks to me, the android app is awesome !!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.