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 { 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; }
Subscribe to our youtube channel to get new updates..!
-----
-(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="../../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="../../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="../../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="../../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="../../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="../../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="../../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="../../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="../../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="../../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