Home  >  Blog  >   iPhone  > 

Creating and Configuring a Table View – iPhone

A table view is a scrollable list of many rows that can be separated into parts. iOS's table views show a single column of rows of vertically scrolling material. An individual section of your app's content is represented by a single row in the table. Learn how to create and setup an iOS Table View.

Rating: 4
  
 
1867

 

Table View:

Table Views  are versatile, User Interface objects frequently found in iOS apps. A table view presents data in a scrollable list of multiple rows that may be divided into sections.

Inclined to build a profession as IOS Developer? Then here is the blog post on IOS Development Training Online.

Table views have many purposes: 

  • To let users navigate through hierarchically structured data.
  • To present an indexed list of items.
  • To display detail information and controls in visually distinct groupings.
  • To present a selectable list of options.

.h file:-

@  interface  TableviewController:  UIviewController

{
IBoutlet   UITableview  *  table view;
NSMutableArray     *    Names;
NSMutableArray    *  Contacts;
Int  count;
}
@ end

.m file:-

-(void) viewDidLoad
{
Tableview.  backgroundColor   =  [  UIColor  ClearColor];
Names= [ NSMutableArray arraywithObjects:
@  “divya”,@  “vamshi”, @ “sony”  @ “santhu”, @ “nani”, nil];
Contacts = [NSMutableArray    arraywithObjects:
@  “123”,  @ “456”, @ “789”, @ “101112”, @ “131415”, nil];
[Super viewDidLoad];
}
Capture 15(NSInteger) numberofSectionInTableview:(UITableview     *  table view
{
return 2;
}

-----

-(NSInteger)  table view:(UITable view    *)  table view
NumberofRowsInSection  : (NSInteger)Section
{
 If (Section = = 0)
return  [ Names   Count];
else
return  [  Contacts   Count];
}
Capture 15(  CGFloat)  table  view:  (UITable  view     *)  tableview
titleForHeaderInSection:  (NSInteger) Section
{
If ( Section = = 0)
return @   “NAMES”;
else
return   @ “Contact No”;
}

Capture 15 -(CGFloat)  table view  : (UITableView     *) table view
HeightFor RowAtIndexpath : ( NSIndex path    *)
IndexPath
{
return   60;
}
Capture 15
-( UITableviewCell    *) table view   :( UITableview    *)
table view   cellForRowAtIndexPath   (NSIndex path     *) index path
{
Static   NSString     * cell  ident   =   @  “cell”;
UITable view cell    * cell     = [table view  Reusable cell with Indentifier: Celident ];
If (cell = = nil)
{
Cell =  [[[ UITableviewcell  alloc]  initWithFrame:
CGRectZero      reuseIdentifier: cellident]  autorelease];
Cell. SelectionStyle  =     UITableviewcellSelectionStyleBlue;
}
else
{
For (UIView    *  view  in cell. Subviews)
{
If([ view  is kindOfClass:  [ UILabel Class]]
    [ view  is kindOfClass: [  UIImageview   class]])
{
    [  view     removeFromSuperview];
}
}
}
   If (indexpath .Section  = = 0)
{
   Cell. textLabel.  text  =   [  Names objectAtIndex: indexpath row]
}
else
{
  Cell .textLabel .text   =[  contacts   ObjectAtIndex:   indexpathrow)
}
  return cell;
}

Table view custom view:-

.h file:-

# import  
@ interface   div table view controller : UIViewController
{
NSMutableArray     *a Array;
IBoutlet   UITableview    * tables;
}
@ end

.m file:-

@ implementation  divtableviewController
-(void)    viewDidLoad
{
Self . view .backgroundColor = [UIColor   blackColor];
       Table . backgroundColor  =   [UIColor   ClearColor];
       array = [[ NSMutableArray   alloc]  initwithObjects:
       @ “red”, @ “orange”, @ “blue”,   , nil];
       [Super view  DidLoad];
}

MindMajix Youtube Channel

# Program Mark Table View Data Source   Methods

Capture 15

-(NSInteger) numberofSectionInTableview: (UITable view *) table view
{
           Return 1 ;
}
Capture 15

-(NSInteger)  table view: (UITableview    *)   tableview
 NumberofRowsInSection  : (nsInteger)Section
{
     return  [aArray   Count];
}
Capture 15

-(CGFloat)   table view: ( UITable view   *) tableview
HeightForRowAtIndexpath: (NSIndex path    *)  index path
{
    return 150;
}
Capture 15

-(NSString *) table view : (UITable view *) table view
titleforHeaderInSection : (NSInteger) Section
   {
     return  nil;
  }
Capture 15

-(UITable view cell *) tableview : (UITable view *) table view
        CellForRowAtIndexpath: (NSIndexpath    *) indexpath
{
Static  NSString   * cellIdentifier  =   @  “Custom cell;
UI Tableviewcell       * cell  =    [table view
DesuetudeReusablecellwithIdentifier  :   cellIdentifier
If( cell   = = nil)
{
Cell  =  [[ UITableviewCell alloc]  initWithStyle:
UITableviewCellStyleSubtitle   reuseIdentifier:
                                  CellIdentifier]  autorelease];
}
else
{
For(  UIview     * view in cell. Subviews)
{
If ([ view is kindOf class: [ UILabel   class])  //
     [ view  is kindOf class  :   [UIButton class]] //
     [ view is kindofClass: [UIImageview  class]])
{
     [ view  removeFromSuperview];
}
}
}
Capture 15

UIImageview   * imgview    =  [[ UIImageview  alloc]
InitwithFrame  :    CGRectMake  (120,50,100,80)];
[  imgview    setImage  :    [  UIImage   imageNamed  :@   “2.  Jpg”];
[   imgview   setTag     :   indexpath  . row];
[ cell addsub view  : img view];
[  img  view   release ];
Capture 15

UIButton   * btn  = [[ UIButton  alloc]   initWithFrame:
CGRectMake( 250,100,60,400)];
[  btn   addTarget: self  action  :    @  Selector  (  button   Clicked)
ForControlEvents:   UIControlEventTouchupInside];
[  btn  set   tag  :   indexPath row];
[  btn set BackgroundImage   :  [UI image  
ImageNamed:  @  “ 1.  Png”]  for state: UI Control state Normal];
[   btn  SetTitle: @  “Btn”  for State :   UIcontrol   stateNormal];
[cell addsub view  :  btn];
[   btn   release];
return  cell;
}
Capture 15

-(void)  buttonClicked : (id)  sender
{
UIButton      * SelectedBtn   =    (UIButton    *)  Sender;
int   tag   =    selectedBtn.tag;
NSLog(  @ “ the Selected  button  index  is %d”, tag);
}

# Program Mark Table View Delegate Method:-

-(void)  table  view:    (UITableview     *)   tableview
Did selectRowAtIndexPath:  (NSIndex path      *)
IndexPath
{
NSLog  ( @  “did selectRowAtIndexpath is called and  index is %d”,   index .row);
}

 

Frequently Asked IPhone Interview Questions & Answers

 

 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
IOS Development TrainingMay 21 to Jun 05
IOS Development TrainingMay 23 to Jun 07
IOS Development TrainingMay 28 to Jun 12
IOS Development TrainingMay 30 to Jun 14
Last updated: 08 Apr 2022
About Author
Ravindra Savaram

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

Recommended Courses

1 /15