First Swift Project

Source: Internet
Author: User
Tags dateformat locale

One, add a cocoa touch class to the new Swift single-page program for storing data and interacting

Import UIKit

Creating a Local Data model
Class Todomodel:nsobject {
var id:string
var image:string
var title:string
var date:nsdate
Init (id:stringimage:string,title:string,date:nsdate) {
Self.id = ID
Self.image = Image
Self.title = Title
Self.date = Date
}
}

Second, outside the class of Viewcontroller, create a global variable as the local runtime database

var todos:[todomodel]=[]

To add initialization data in Viewdidload ():

Todos = [Todomodel (ID: "1", Image: "child-selected", Title: "1. Go to the Playground", date:datefromstring ("2016-1-1")!),
Todomodel (ID: "2", Image: "shopping-cart-selected", Title: "2. Shopping", date:datefromstring ("2016-1-2")!),
Todomodel (ID: "3", Image: "phone-selected", Title: "3. Call", date:datefromstring ("2016-1-3")!),
Todomodel (ID: "4", Image: "travel-selected", Title: "4. Travel to the Yang Luo", date:datefromstring ("2016-1-4")!)

To facilitate the processing of nsdate variables, create two global methods outside of class

Func datefromstring (datestr:string)->nsdate? {
Let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = "Yyyy-mm-dd"
Let date = dateformatter.datefromstring (DATESTR)
Return date
}
Func stringfromdate (date:nsdate)->string{
Let locale = Nslocale.currentlocale ()
Let DateFormat = Nsdateformatter.dateformatfromtemplate ("Yyyy-mm-dd", options:0, Locale:locale)
Let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = DateFormat
return Dateformatter.stringfromdate (date)
}

Third, use the TableView control to display the data in the database

1. Open Storyboard, drag control TableView in view, add cell in TableView

2. Change the cell's style property to basic while changing the title content

3. Drag lines to Viewcontroller to create a TableView property

4. To configure TableView properties, let Viewcontroller inherit the Uitableviewdatasource protocol

5. Two ways to implement the Protocol:

Set the number of rows for TableView
Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), int{
return 20
}
Set up a multiplexed cell
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), uitableviewcell{
Let cell = Self.tableview.dequeueReusableCellWithIdentifier ("Todocell")! As uitableviewcell//remove cell
return cell
}

6. In order to connect TableView and Viewcontroller, go back to storyboard, left column, drag line from TableView to Viewcontroller, select DataSource

Four, customized TableCell

1. Set the cell's style to custom

2. Change the height of the cell to add controls such as Imageview,lable

Five. Bind data in model to TableView

1. Change the setup TableView line number code as follows

Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), int{
return todos.count//The number of data in the database is dynamically displayed in TableView
}

2. Change the code for the previous method of reusing cells as follows

Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), uitableviewcell{
Let cell = Self.tableView.dequeueReusableCellWithIdentifier ("Todocell")! As uitableviewcell//remove the target cell

Gets the database and the control that holds the data, in order to easily remove the control in the cell, the control tag in the cell is assigned a value of 101,102,103
Let Todo = Todos[indexpath.row] as Todomodel
Let image = Cell.viewwithtag (101) as! Uiimageview
Let title = Cell.viewwithtag (102) as! UILabel
Let date = Cell.viewwithtag (103) as! UILabel
Passing data from a database to a control
Image.image = UIImage (named:todo.image)
Title.text = Todo.title
Date.text = Stringfromdate (todo.date)
Returns the configured cell
return cell
}

Six, give TableView add delete function

1. To configure TableView behavior, let Viewcontroller inherit the Uitableviewdelegate protocol

2. How to implement the Delete method in the protocol:

Add Delete method
Func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath Indexpath:nsindexpath) {
Capturing Delete operations
if Editingstyle = = uitableviewcelleditingstyle.delete{
Remove the selected data
Todos.removeatindex (Indexpath.row)
Add Delete animation
Self.tableview.deleteRowsAtIndexPaths ([Indexpath], withRowAnimation:UITableViewRowAnimation.Automatic)
}
}

3. To avoid users not knowing the delete gesture, add the Edit button:

Add navigation to Viewcontroller and override the method of adding edit buttons

Add Edit button
Override Func setediting (Editing:bool, Animated:bool) {
Super.setediting (editing, animated:animated)
Self.tableview.setEditing (editing, animated:animated)
}

  

First Swift Project

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.