Now let’s see about UITableView. Create a project and navigate it to home screen.
In home screen header file (homescree.h) file, declare NSMutableArray for table data as mentioned in below code. I usually use NSMutableArray instead of NSArray because NSArray is non editable which means once if you initialize it with data’s, you cannot insert or delete the data’s in NSArray where as it is possible in NSMutableArray.
UITableViewDataSource and UITableViewDelegate are the protocols of Objective C which are used to display table view.
Then, in main file (homescreen.m) synthesize the table view and array which we declared in header file as mentioned below.
Synthesize will allows user to access the fields and variables declared in header file.
Allocate the array value in the initWithNibName so that when the application starts, it will automatically hit the initWithNibName, and it will allocate the array.
As we already know, viewDidLoad is the constructor add values to the array in viewDidLoad as mentioned below
Now it’s time to implement table view. Implementing a simple table mainly involves 3 functions which are mentioned below
• numberOfRowsInSection (used to declare row count of table)
• cellForRowAtIndexPath (used to declare cell text of table)
• didSelectRowAtIndexPath (table cell onclick function)
First we will initialize the number of rows for the table in numberOfRowsInSection function as mentioned below
In the above code, I have declared the array count as row count of the table.
Now we have to assign the array values to the table cells
In the above function, each and every line of code are explained with commented lines. Adding thumbnail image icon is optional. I don’t want to display the table view plain. To add spices, I have added the icon to it.
Now we have to implement onclick function for the table view
Now all the coding part is over. In application development we may declare several variables. Once we allocate a variable, surely it has to be de-allocated to free the memory. In our case we have allocated array. Now we have to release the array as mentioned below
So, coding part is over. Here comes the designing part. Open the home screen XIB file (homescreen.xib) and add UITableView as shown in the below image
Now if you run the application, you can able to see the table view in output. But there will be no data in the table view because we have not assigned the data source and delegate. It has to be done as shown in the below image
Here the datasource is linked to the file’s owner
Here the delegate is linked to the file’s owner
Now your table view will look like this.
Now save and run your application. Your output scree will look like this
In addition to the table view tutorial, I will show you how to add application icon to your application.
Click on the root header of your project directory as shown below
And you will get the project summary screen. Then you have to right click on the app icon to browse the image in file path as shown below
Now select your desired app icon. Note, your app icon should exactly in size of 57 x 57.
Select your image and the app icon in summary screen will look like this
Now run your application. You got your app icon.