본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

Custom UITableViewCell example in iOS

by 테크한스 2015. 9. 19.
반응형

Custom UITableViewCell example in iOS

       We have already worked on implementing an UITableView in ios development. So now let’s move further. In this tutorial I am going to implement custom UITableViewCell as an additional part of the previous UITableView tutorial.

            UITableView is an collection of UITableViewCell’s. It defines the attributes and behaviour of the cells. UITableViewCell may contain image, text, label or whatever. User can use the default cell or he/she can modify them as per their requirement in the app development process. Read apple documentation about UITableViewCell.

uitableviewcell

Overview

    In this tutorial I am going to create an application with customized UITableViewCell’s, which contains food recipe image, name and their preparation time. 

Getting Started

      Create a new single view application. Enter the project name as ‘CustomUITableViewCell’. Enter the organization and company identifier details. Select the device family as ‘iPhone’. Select next and save the project.

createProject_tableviewcell.png

 

           Now your project should contains these files,

fileList.png

 

         Select Main.Storyboard file. Drag and drop an UITableView in the viewcontroller scene. Create an outlet for the UITableView as ‘tableViewObject’ and connect the datasource, delegate to the file owner.

tableviewoutlet.png

 

        Add this code inside the ViewController.h file. Here we are declaring three array’s to display recipe image, name and time needed to prepare them. 

 

        Now add this code below inside the ViewDidLoad method in the ViewController.m file.

 

     Oops! We need to add the recipe images. Download and add these recipe images inside your project. These images will be displayed in the UITableViewCell. 

 

Create Custom UITableViewCell

        Select File–>New–>File to create a new file. Enter the file name as ‘CustomCell’. Select the SubClassof as ‘UITableViewCell’. Uncheck the options ‘Targeted for iPad’ and ‘With XIB for user interface’ and select next to save the files.

createNewFile_uitableviewcell.png

 

          Now we need to create a xib file. Select File–>New–>File. Select file type as user interface–> Empty. Select next and save the file.

selectEmpty_uitableviewcell.png

         Drag and drop an UITableViewCell on the xib. Now add an UIImageView and two labels for recipe name and time inside the UITableViewCell. Now your xib file should look like this,

setCustomClass.png

          Now you need to connect your new xib file to CustomCell class. Select the root view under object in the xib file. In the third column of the file inspector you can find a field named as ‘Custom Class’. Enter the value as ‘CustomCell’ the newly created class name.

uitableviewcell

      UITableView does not destroy every cell after using them. It will recycle these cell’s to improve their performance. To do this UITableView needs to find the specific cell. So the Cell Identifier is used to identify a particular cell. Even you can  implement multiple cell’s for an UITableView.

         Now select the root view under objects in the xib file. You can find the field named ‘Identifier’ on the fourth column of the file inspector. Enter the value as ‘CustomCell’.

uitableviewcell

     

      Create IBOutlets for UIImageView, recipe name label and time label as ‘imageView’,’foodName’ and ‘time’. Add @synthesize to these IBOutlets in the CustomCell.m file like this,

Implement UITableView

       We have already created our custom UITableViewCell so its time to implement the UITableView. Add these methods inside the file ‘ViewController.m’.

 

       Implement the ‘UITableViewDelegate’ and ‘UITableViewDataSource’ in the ViewController.h file. Like this,

delegateImplement.png

 

Conclusion

   In the above code everything is implemented pretty same as UITableView implementation. The only difference is we have implemented the CustomCell instead of UITableViewCell in the ‘cellForRowAtIndexPath’ method. Now everything seems to work fine. You can download the source code below to understand this complete app development process.


반응형