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