Mindmajix

Creating and Configuring a Table View – iPhone

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.

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
<UITableviewDelegate,  UITableviewDataSource>
{
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;
}

Capture 15

-(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
HeightForRowAtIndexpath : ( 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  <UIKIT/UIKIT .h>
@ 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

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

 

 


 

0 Responses on Creating and Configuring a Table View – iPhone"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.