Tài liệu Thủ thuật lập trình cho iPhone part 5 pptx

19 406 1
Tài liệu Thủ thuật lập trình cho iPhone part 5 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Collection By traibingo 46 Bài 6: Gắn UITableView với một NSArray (Populating UITableView With An NSArray) Phần này sẽ hướng dẫn làm cách nào để bind một cái mảng vào một cái table trong iPhone. Trong iPhone thì đối tượng UITableView là một thể hiện của một đối tượng NSArray, tức là dữ liệu trên NSArray được hiển thị lên UITableView. UITableView không thể hiện được mảng 2 chiều bằng cách chia thành nhiều row và column mà cơ bản thì nó chỉ có một column duy nhất và trên column đó chúng ta sẽ có nhiều đối tượng UITableViewCell là thể hiện của một row tức là 1 cell. Chúng ta có thể customize (tuỳ biến) cái UITableViewCell này theo ý muốn vì bản chất nó cũng được kế thừa từ một UIView. Dưới đây là nguyên bản bài tutorial được lấy từ iCodeBlog, tôi không dịch nữa vì nó rất dễ đọc và rất dễ hiểu. <script type="text/javascript"></script><script src="http://digg.com/api/diggthis.js"></script>The goal of this tutorial is to show you how to populate a UITableView with data from an array of objects. This will be the building block to display XML data as well as SQL data. The theme of this application will be fruit. We will create an array of “fruit” objects that have some additional information besides the name. We will populate a UITableView with the names of the fruits. When the user selects a fruit from the list, the view will transition to another one and display specific details about that fruit. I will try to be as detailed as possible however it would be useful if you have completed the following tutorials as I will use concepts from each of them:  UITableView Hello World  Connecting Code To An Interface  Transitioning Between Views In this tutorial you will learn: Collection By traibingo 47  Create a Navigation-Based Application  Create a New Fruit Class Object  Create and Populate an NSArray  Add a New View To Your Project  Connect The View To Code  Populate A UITableView With Object Data  Transition To New Views And Show Data Based On The Row Selected Open up X-Code and Select File->New Project… Select Navigation- Based Application and click Choose… Name your project Fruit. We are going to create our “fruit” objects that will be used in the application. If you are not too familiar with object oriented programming…Google it. That tutorial would be a little too huge for me to write. Click on File -> New File… The object we are creating will inherit from NSObject, so select NSObject Subclass and click Next. Collection By traibingo 48 The next screen will ask you to name it. Go ahead and name it “Fruit” and make sure that “Also create Fruit.h” is checked. It should look like the screen below. Then, click Finish. Collection By traibingo 49 Now, we are going to define the properties of a “Fruit” object. For this application a fruit will have a name and a description. Open Fruit.h and add the following code: We have created the properties needed to represent our fruit. There is one line that you may be unfamiliar with. The line-(id)initWithName:(NSString*)n description:(NSString *)desc; is a definition for a function. This function will be called to initialize a Fruit object. All NSObjects have an init method, but we want to create our own so that we can pass in a name and description when the object is created. Open up Fruit.m and add the following code: Here we implement the initWithName method. The code here seems pretty straight forward. We are basically setting our local copies of name and description to the arguments passed to this method. The important thing to notice here is the return self line. This is crucial for using this method as a constructor. It allows this function to return the newly created instance of a fruit object. Collection By traibingo 50 Next, we are going to set the title of our main view. This is necessary to create a back button when transitioning between views. Open up RootViewController.m…In the viewDidLoad method, add the following code: We are setting the title property of the RootViewController object to the string “Fruits”. Also, be sure to add the #import “Fruit.h” line at the top to include the fruit object in our project as well as @synthesize fruitView to add the “getter” and “setter” methods. Next, we are going to create an array of fruit objects. Open up FruitAppDelegate.h and add the following code: All we are really adding here is an NSMutableArray property. I used NSMutableArray instead of NSArray because it has a few more methods making it more flexible. Now, open up FruitAppDelegate.m and add @synthesize fruits to the top. This is so other objects will have access to the fruits array. Also, be sure to include the import statement for Fruit.h. Collection By traibingo 51 Now add the following code to the applicationDidFinishLaunching method. What we are doing here in the 1st three lines is creating new instances of a fruit object. Notice that instead of calling init, we are calling the initWithName method that we created. This is allowing us to pass in a name and a description to each of the fruits. The next line [self.fruits = [[NSMutableArray alloc] initWithObjects:apple,orange,watermelon,nil]; builds a new array from the objects we just created. It is important to pass in nil as the last argument in an NSMutableArray. It will not work unless you remember this. Now we are going to create the view that will be displayed when the user selects a fruit. Double click on any one of your .xib files to open up Interface Builder. Collection By traibingo 52 Click File -> New and select view and click choose. You should now see the new view (it will be blank) and the objects associated with it. We need some sort of text field to display the description of the fruit. For this tutorial I have chosen a UITextView as opposed to a UITextField. This is because a UITextView is multi-line and is great for Collection By traibingo 53 displaying more than one line of text. So, go ahead and drag it on to your view. Your view window should now look like this: Now click File -> Save. One important thing to keep in mind is to make sure you are saving it in the current project‟s directory. It seems that every time I add a view to a project, the default directory is not my project‟s directory. Name this file FruitViewController and click Save. Collection By traibingo 54 Another window should show up after you click save. It is asking you if you want to add this view to your project. Just check the box net to Fruit ,click Add, and close Interface Builder. Close Interface Builder and go back to X-Code. Collection By traibingo 55 We need to create a ViewController to handle our View. Click File -> New File… Select UIViewController subclass and click Next. Advertisement <script type="text/javascript"> //<![CDATA[ ord = window.ord || Math.floor(Math.random()*1E16); document.write('<script type="text/javascript" src="http://ad4.netshelter.net/adj/ns.icodeblog/general;kw=;tile=4;sz=300x2 50,336x280;ord=' + ord + '?"><\/script>'); //]]> </script> Name this file FruitViewController.m and check the box that says “Also create “FruitViewController.h” . Click Finish. [...]... FruitViewController class we just created Click on the File’s Owner object Collection By traibingo 56 Click Tools -> Identity Inspector Select FruitViewController from the dropdown next to class Notice that under the Class Outlets section you will see the UITextView property we created Collection By traibingo 57 The last step in interface builder is to connect the UITextView Click Tools -> Connection Inspector... over the UITextView on your view and release it Collection By traibingo 58 Now, click the circle next the word View under outlets and drag it to the View object inside of the window that says FruitViewController in the title When you are done the screen should look like this: Now close Interface Builder Collection By traibingo 59 The first thing we are going to do here is create a property for our... that you got a lot out of this tutorial As always, if you have any questions or comments, feel free to leave them in the comments section of this post We also have a forum to help you will all of you iphone related questions If you get lost, you can download the sample code here Collection By traibingo 64 . vào một cái table trong iPhone. Trong iPhone thì đối tượng UITableView là một thể hiện của một đối tượng NSArray, tức là dữ liệu trên NSArray được hiển. Interface Builder. Collection By traibingo 52 Click File -> New and select view and click choose. You should now see the new view (it will

Ngày đăng: 21/01/2014, 14:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan