Blog

  • Home
  • iPhone
  • Creating and Configuring a Table View – iPhone

Creating and Configuring a Table View – iPhone

  • (4.0)
  • | 1023 Ratings

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”;
}

<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="Capture 15" /> -(CGFloat)  table view  : (UITableView     *) table view
HeightFor RowAtIndexpath : ( NSIndex path    *)
IndexPath
{
return   60;
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="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];
}

# Program Mark Table View Data Source   Methods

<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="Capture 15" />

-(NSInteger) numberofSectionInTableview: (UITable view *) table view
{
           Return 1 ;
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="Capture 15" />

-(NSInteger)  table view: (UITableview    *)   tableview
 NumberofRowsInSection  : (nsInteger)Section
{
     return  [aArray   Count];
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="Capture 15" />

-(CGFloat)   table view: ( UITable view   *) tableview
HeightForRowAtIndexpath: (NSIndex path    *)  index path
{
    return 150;
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="Capture 15" />

-(NSString *) table view : (UITable view *) table view
titleforHeaderInSection : (NSInteger) Section
   {
     return  nil;
  }
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="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];
}
}
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="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 ];
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="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;
}
<img class="alignnone wp-image-27245" style="border: 0px; box-sizing: border-box; color: #666666; font-family: helvetica neue,helvetica,arial,sans-serif; font-size: 14px; height: 19px; max-width: 100%; text-align: justify; vertical-align: middle; width: 19px;" src="https://mindmajix.com/blogs/images/rightarrow.png" alt="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



Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Ravindra Savaram
About The Author

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.


DMCA.com Protection Status

Close
Close