In the part I of UITableView, we are not really going to use the cells in the table. Now, we are trying to do so in a very simple way.
Firstly, create a View-based Application:
In Classes group, we add a new file for our simple table's view controller:
We select UIViewController subclass, and please do remember to tick UITableViewController subclass option in Options section:
Give it a name, say MyTableViewController:
Double-click MyTableViewController.xib to take a look on what it's inside:
You may right-click the Table View and see that it already has three relations with File's Owner (MyTableViewController). They are for table's data feeding and view management. If we don't tick UITableViewController subclass option in Options section while making a new view controller class, those Table View and relations won't be automatically generated, and you should make it yourself.
Let's go back to our newly created view controller MyTableViewController, and jump to numberOfSectionsInTableView: method:
This function will be called every time the table refresh / reload the data. It asks for the number of sections of the table. Let's return 1 for this method:
Let's jump to tableView:numberOfRowsInSection: method:
This method will be called by the table and we have to return the number of rows in a specified section of the table. In our case, we only have 1 section, so we should directly return the number of rows, without considering which section we are dealing with. For example, we return 10:
Now, we are really making our cells to print something out. Let's jump to tableView:cellForRowAtIndexPath: method:
Let's add a line just below "// Configure the cell..." comment:
This line set the text attribute of a UILabel instance to a NSString instance which is the index number (0 to 9) of a certain row.
Now, we will add a view controller class in MainWindow.xib. Double-click it in Resources folder:
Open Library window from "Tools -> Library". Drop a View Controller class to MainWindow.xib:
With the newly added View Controller item selected, open Inspector window by clicking "Tools -> Inspector":
Assign NIB Name in View Controller Attributes tab:
Assign Class My Table View Controller Identity tab:
Let's add a class member of MyTableViewController in TableViewTestAppDelegate.h:
Also, add something for synthesizing getter and setter in TableViewTestAppDelegate.m:
Add a line:
@synthesize myTableViewController;
This will generated setter and getter of myTableViewController for you.
Also, jump to dealloc method for memory issue:
Let's add a line to release the view controller:
Go back to MainWindow.xib again. Make a relation between TableViewTest App Delegate with File's Owner. Dragging New Referencing Outlet onto it and select myTableViewController we defined before in TableViewTestAppDelegate.h:
Go back to TableViewTestAppDelegate.m file and jump to application:didFinishLaunchingWithOptions: method:
Remark the original one as shown and add a new line to it:
Now, build and run the App on the simulator:
Finally, a table of 10 rows showing row indices is shown.
沒有留言:
張貼留言