This tutorial is based on a previous project about Storyboard creation. You will learn how to use the design pattern, work with Fundaction, and write a custom class to add dynamic data to support your todolist application.
This tutorial teaches you how:
1. Basic work
2. Create a custom data class
3. Implement a delegate and Data Source Protocol
4. data between view Controllers
5. After you complete all the steps in this tutorial, you will have an application that looks like this:
Create a data class and start using it. Open an existing project in Xcode.
At this point, you must use? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> encrypt + 3bTmtKKjrLKi0 + encrypt/serKx7S0vai0/encrypt/decrypt + CjxzdHJvbmc + encrypt/decrypt + examples/yo6zM4cq + xPrRodTx0MLOxLz + examples/examples + 8Tj0rvWsbj6y + examples/uxL/examples/samples + Summary + 31 sSsyM /release + CjEwLrWlu/release/NPQxuTD + release/aoaM8YnI + Cgo8YnI + release/2qOs1eLR + bXEyfnD97 + 0 xvDAtM/x1eLR + release = "brush: java; "> @ interface XYZToDoItem: NSObject @ property NSString * itemName; @ property BOOL completed; @ property (readonly) NSDate * creationDate; @ end
Key Point: To generate a project, select Product> Build (or press Command-B ). You don't use your new class to do anything, but building it gives the compiler A Chance To verify that you haven't made any typing errors. If you have, fix the warning or error readings they provide through the compiler, and then look back at the instructions in this tutorial to make sure everything looks as it describes here. Now you have a class to add data. You can create and store data for a single list item. You also need to keep the list of these items. Naturally, this is where XYZToDoListViewController Class View controllers are responsible for coordination between models and views. Therefore, they need a reference model.
The Foundation framework contains a class named NSMutableArray, which provides an effective project tracking list. Variable array is very important, so that you can add the project to the array. An unchangeable version. NSArray does not allow you to add a project to the worker after its initialization.
If you want to use it, you need two declarations and create an array. You can do this by allocating and initializing arrays.
Allocate and initialize Arrays
1. In the project navigator, select XYZToDoListViewController. m.
Because the project array is the implementation details of your table View Controller, you are in the m file instead. Declare it in the H file. This makes it a private custom class.
2. Add the following attributes to Xcode of the interface class created in your custom table View Controller class. The statement should be as follows:
@interface XYZListViewController () @property NSMutableArray *toDoItems; @end
3. Assign and initialize the viewDidLoad method in the toDoItems array:
- (void)viewDidLoad{ [super viewDidLoad]; self.toDoItems = [[NSMutableArray alloc] init];}
When some additional lines are inserted to the actual code in viewDidLoad by Xcode, it creates XYZListViewController and is commented out. Welcome to stay with them.
At this point, you have an array where you can add projects. You will do this in a separate method, loadInitialData, which you will call from viewDidLoad. This code is put in your own method, because it is a modular task, you can improve the readability of the Code by making this method alone. In a real application, this method will load data such as files that are persistently stored. Now, our goal is to see how the table view applies to custom data items, so you will create some test data for the experiment.
Allocation and initialization: Create a project in the way you create an array. Then, enter the project name. This is the name that will be displayed in the table view. Several projects that achieve this.
Load initial data1. Add a new method, loadInitialData, And the @ implementation line below.
- (void)loadInitialData {}2. In this method, several list items are created and added to the array.
- (void)loadInitialData { XYZToDoItem *item1 = [[XYZToDoItem alloc] init]; item1.itemName = @"Buy milk"; [self.toDoItems addObject:item1]; XYZToDoItem *item2 = [[XYZToDoItem alloc] init]; item2.itemName = @"Buy eggs"; [self.toDoItems addObject:item2]; XYZToDoItem *item3 = [[XYZToDoItem alloc] init]; item3.itemName = @"Read a book"; [self.toDoItems addObject:item3];}
3. Call the loadInitialData method in viewDidLoad.
- (void)viewDidLoad{ [super viewDidLoad]; self.toDoItems = [[NSMutableArray alloc] init]; [self loadInitialData];}
Key Point: Select Product> Build. You should see many lines with errors in your loadInitialData method. The key is the first line, which should be said to be "using the undeclared identifier XYZToDoItem ." This means that when the compiler does not know about your XYZToDoItem, it compiles XYZToDoListViewController. The compiler is very special and needs to be clearly told what to pay attention.
Tell the compiler to pay attention to your custom list item class
1. Find the first line of the # import "XYZToDoListViewController. h" near the XYZToDoListViewController. m file.
2. Add the following lines next to them:
#import "XYZToDoItem.h"
Key Point: Select Product> Build to generate a project. It should be created with no errors. Display data at this point, your table view is pre-filled with some sample To-Do variable arrays. Now, you need to display data in the table view.
You can do this by making the data source of the XYZToDoListViewController table view. To make some data sources of table views, it needs to implement the UITableViewDataSource protocol. It turns out that the method you need to implement is commented out in the second tutorial. There is a menu view that requires three methods. The first item is numberOfSectionsInTableView, which tells the table view how many parts are displayed. For this program, you want the table view to display a single part, so the implementation is very simple.
To display a part of the table
1. In the project navigator, select XYZToDoListViewController. m.
2. If you comment out the method of the data source in the second tutorial table view, delete these annotation tags now.
3. Find the template implementation part that looks like this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{#warning Potentially incomplete method implementation. // Return the number of sections. return 0;}
You want to have a single part, so you want to delete the cordon and return value from 0 to 1.
4. Change numberOfSectionsInTableView: the data source method returns a single part, as shown in the following code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return 1;}
The implementation code of the next method is as follows: numberOfRowsInSection: tells the table view how many rows are displayed in the given section. In a single part of your table, each to-do list should be in its own row in the table view. This means that the number of rows should be the number of toDoItems arrays in the XYZToDoItem object.
Returns the number of rows in the table.
1. In the project navigator, select XYZToDoListViewController. m.
2. Find the template implementation, which looks like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{#warning Incomplete method implementation. // Return the number of rows in the section. return 0;}
You want to return to the number of items in the list. Fortunately, NSArray has an easy-to-use method named count to return the number of items in the array, so the number of rows is [self. toDoItems count].
3. Change the implementation code as follows: numberOfRowsInSection: the number of rows returned by the data source method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return [self.toDoItems count];}The implementation code of the last method is as follows: cellForRowAtIndexPath:. A cell is required to be displayed as a given row. Until now, you have been working and only have code, but the cell of the row to be displayed is an important part of your interface. Fortunately, Xcode can easily design custom cells in Interface Builder. The first task is to design your mobile phone and tell the table view, instead of using static content. It is the prototype cell and dynamic content to be used.
Configure your table View
1. Open your storyboard.
2. Select the outline table view.
3. Select from Table view to open the property checker in the tool area.
4. In the property checker, modify the content attributes of the table view, from static to cell dynamic prototype.
In Interface Builder, You need to configure static cells and convert them into all prototypes. Prototype cells, as the name implies, are cells that are configured with text styles, colors, images, or other attributes, as long as you want them to display but get data from the data source at runtime. Load each row of a prototype battery from the data source, and configure the cell to display the data of this row.
When loading the correct cell, the data source needs to know what it is called and the name must also be configured in the script.
When you set the prototype unit name, you can also configure the style selected by other property communities. It determines the appearance of cells when users click it. Set the cell style to none, so that the cells clicked by a user will not be highlighted. This is what you want to do when a user clicks on a project's to-do list to be completed or not yet completed-a feature that will be implemented later in this tutorial to commemorate its cell behavior.
Configure prototype Cells
1. Select the first table view cell in the table.
2. In the property checker, locate the Identifier Field and type ListPrototypeCell.
3. In the property checker, locate the selection field and select none.
You can also change the font or other properties of the prototype cell. The basic configuration is easy to use, so you will maintain this.
The next step is to teach your data source how to implement the given line of the following Configuration unit in the Code: cellForRowAtIndexPath :. This data source method is called a table view when it wants to display a given row. For a table view with a small number of rows, all rows may be on the screen once, so this method is called for the table in each row. However, a table view with a large number of rows shows a small part of its total project at a given time. This is the most effective table opinion. Only the rows displayed in the cell are required. The implementation code is as follows: cellForRowAtIndexPath: Allows table views.
For any given row in the table, take the corresponding entries in the toDoItems array, and set the text label of the cell to the project name.
Table displayed in Cells
1. In the project navigator, select XYZToDoListViewController. m.
2. Find the implementation code: cellForRowAtIndexPath: data source method. The template implementation looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... return cell;}
This template executes multiple tasks. It creates a variable to save the cell of the identifier, and requires the table view of the cell of the identifier. With the addition of the code, you can comment out the unit configuration, then return the cell.
To make the code of your application work, you need to change the identifier to a cell that you set in the script, and then add the code to configure.
3. Change the cell identifier in one of the script settings. To avoid typos, copy and paste them to the script execution file. The ID row in this area should now look like this:
static NSString *CellIdentifier = @"ListPrototypeCell";
4. Add the following code line before the return statement:
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];cell.textLabel.text = toDoItem.itemName;
Your tableView: cellForRowAtIndexPath: this is the case:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"ListPrototypeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row]; cell.textLabel.text = toDoItem.itemName; return cell;}Key Point: Run your application. The project list you added to loadInitialData should be displayed as a cell in the table view.