IOS Core Data Practice 1

Source: Internet
Author: User

1, create a new project, tick core data

2, delete the default Viewcontroller, add a new Table View controller, and create a class for it Tableviewcontroller:uitableviewcontroller

3, embed a navigation Controller for table View Controller

4, embed a bar Button,system the Item property in the upper right corner of the table view controller's navigation bar to add

5. Add a "AddName" event to the Add button by dragging the line

6, create a new array in Tableviewcontroller to save the data:

var names = [String] ()

7, set the Identyfier property of the cell in TableView to "cell"

8,viewdidload, set page title

title = "List of names"

9, Configuration TableView

Numberofsectionsintableview:

Return 1

Numberofrowsinsection:

Return Username.count

Cellforrowatindexpath:

Let cell = Tableview.dequeuereusablecellwithidentifier ("cell", Forindexpath:indexpath)
Cell.textlabel?. Text = Names[indexpath.row]
return cell

10, write the Add method, add the data

@IBAction func addname (sender:anyobject) {
Let alert = Uialertcontroller (title: "Add new name", Message: "Please enter a name", PreferredStyle:UIAlertControllerStyle.Alert)
Let saveaction = uialertaction (title: "Save", Style:UIAlertActionStyle.Default) {(action:uialertaction), Void in
Let textfiled = alert.textfields! [0] as Uitextfield
Self.names.append (textfiled.text!)
Let Indexpath = Nsindexpath (forrow: (self.names.count-1), insection:0)
Self.tableView.insertRowsAtIndexPaths ([Indexpath], withRowAnimation:UITableViewRowAnimation.Automatic)
Print ("Deposit a piece of data")
}
Let cancelaction = uialertaction (title: "Cancel", Style:UIAlertActionStyle.Default) {(action:uialertaction), Void in
Print ("User canceled input")
}
Alert.addaction (saveaction)
Alert.addaction (cancelaction)
Alert.addtextfieldwithconfigurationhandler {(Textfield:uitextfield), Void in
}
Self.presentviewcontroller (Alert,animated:true,completion:nil)
}

Here, you can add data, but the data cannot be saved permanently

  

IOS Core Data Practice 1

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.