IOS-create a table application drill using uitableview (1) -- a simple table application-V3.0

Source: Internet
Author: User

Reprinted: http://www.cnblogs.com/liufan9/archive/2012/06/01/2528714.html

Uitableview controls are available in many IOS apps. Let's start our series of tutorials with uitableview.

Target

Any great iOS app comes from a great idea. Although we only want to do rehearsals again, drills also require a great idea! In this way, we will have more strong interests, clearer goals, and no worries about specific details.

Tip: the details are gradually learned through repeated attempts. It is easy to get lost when you enter the details too early.

Now there are more and more people using Weibo. Suppose we need an application that manages the "weibo followers" list to help me maintain the information of weibo followers. How is it? For a beginner in iOS, this assumption should be big enough. :]

Let's get started. 1. Create an application

For how to create an application, see the first IOS Application -- Hello world!, written two days ago !, I will not describe it here. When creating an application, use "my focus" as the product name. As shown in:

2. Add a table control.

1. Open the "mainstoryboard. storyboard" file;

2. To create a table application, and the default storyboard is a view application, click "View Controller" to delete it, as shown in:

After deletion, we will find a blank area, as shown in:

3. Add the table view control. Find the table View Controller Control in the "tool area" on the right and drag it to the blank area of the storyboard;

4. Click the folding tag on the left of "Table View Controller" to expand and select "Table view cell", and then enter "userinfocell" in "identifier" in the attribute area, as shown in:

OK. Let's run it and see what effects have been achieved after the comparison:

An empty table jumps above the screen, and tries to drag it by hand, the table will scroll up and down with the gesture. So far, we haven't written a piece of code, right. :]

3. Preparations before Encoding

Before writing a program, we need to make some simple preparations.

To write code, there must be a "location". In iOS development, there are usually two "write" locations.

1. ". H" file-header file, used to declare object attributes and methods;

2. ". m" file-object implementation file, used for object implementation.

Well, let's keep these two in mind. If we can't use them, let's assume that we will, so that we can have a strong heart and focus on what we want to do. :]

Let's take a simple look at what we did in Part 2: Open "mainstoryboard"-> Delete the default "View Controller"-> Add a "Table View Controller ", because what we need to do is a table application.

Since we want to create a table application, we need a table implementation object. First, let's look at the content in the navigation area, for example:

We can see two files. From the name, one is the View Controller header file, and the other is the view controller implementation file. These two files are obviously not what we need. delete them!

Follow these steps:

1. right-click the GUI and select "new file..." from the context menu... ", select" cocoa touch "and" objective-C Class "in the pop-up window, and click" Next ". For details, see:

2. in the next window, enter "joytableviewcontroller" in "class" and "subclass of" in the drop-down list to select or enter "uitableviewcontroller". Do not select the following two check boxes, click "Next". For details, see:

3. In the next window, click "CREATE" to create an object. The current navigation area should be shown as follows:

Through the above three steps, xcode helps us prepare the header file and implementation file.

Before writing code, you also need to do a very important thing-Tell storyboard that it should follow the code in the newly added object to execute the action.

1. Open "mainstoryboard. storyboard ";

2. Click "Table View Controller ";

3. Click "show the identity inspector" in the tool area to display the "Mark checker ";

4. In "class", click the drop-down button and select the class "joytableviewcontroller" We just created ".

See:

Well, after a few simple preparations, are we going to enter the actual implementation stage? Are you excited? :]

4. Fill the table with code

Click "joytableviewcontroller. M" in the "navigation area" to open it.

Note: During this drill, all codes are only in this file.

Tip: Click the option + command + 0 key combination. You can disable the "tool area" to make the "edit area" look larger.

At the beginning of the Code, there is a @ interface joytableviewcontroller. we can define the private attributes and methods of the object here, instead of in. h file. This feature ensures that your open interfaces are clearer and more concise.

1. Define an array to store the information of the followers. The Code is as follows:

1 @interface JOYTableViewController () {2 @private3     NSArray *_userList;4 }5 6 @end

Tip: Do not forget the braces.

2. Fill in the array content, find the viewdidload method, and add a sentence at the end of the method:

1 _ userlist = [[nsarray alloc] initwithobjects: @ "apps", @ "", @ "", 2 @" ", @ "Kai-Fu Li", nil];

Suppose I only pay attention to these five people.

3. modify the number of table sections, find the numberofsectionsintableview method, and change "Return 0" to "return 1", as shown below:

1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView2 {3     return 1;4 }

The Section content will be described in the next article. Now we assume that the table has only one section.

4. modify the number of table rows and find

1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Method, change return 0:

1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section2 {3     return [_userList count];4 }

Note: the syntax of objective-C is similar to that of natural language. "is replaced by" space "and" brackets.

Notice: I plan to write an article about basic syntax of objective-C after the rehearsal topic of tableview. :]

5. display the array content in cells in sequence and find

1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Method, add the following code before the "Return cell" statement:

1 // instantiate the Cell Object 2 if (cell = nil) {3 cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier]; 4} 5 6 // set the cell text 7 [cell. textlabel settext: [_ userlist objectatindex: [indexpath row];

6. Run the command to see the effect. For details, refer to:]

V. Summary

This drill has come to an end. So far, we have created a simple table application. Although it looks rough, we have taken a solid step. :]

Source program of this drill: joyiosmyfocus1.zip

In the next article, we will show you how to load table content through a data file and display images and other content in the table. Coming soon. :]

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.