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.
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:
.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”; }-(CGFloat) table view : (UITableView *) table view HeightFor RowAtIndexpath : ( NSIndex path *) IndexPath { return 60; }
-( 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; }
.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]; }
-(NSInteger) numberofSectionInTableview: (UITable view *) table view { Return 1 ; }
-(NSInteger) table view: (UITableview *) tableview NumberofRowsInSection : (nsInteger)Section { return [aArray Count]; }
-(CGFloat) table view: ( UITable view *) tableview HeightForRowAtIndexpath: (NSIndex path *) index path { return 150; }
-(NSString *) table view : (UITable view *) table view titleforHeaderInSection : (NSInteger) Section { return nil; }
-(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]; } } }
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 ];
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; }
-(void) buttonClicked : (id) sender { UIButton * SelectedBtn = (UIButton *) Sender; int tag = selectedBtn.tag; NSLog( @ “ the Selected button index is %d”, tag); }
-(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
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
IOS Development Training | May 21 to Jun 05 | |
IOS Development Training | May 23 to Jun 07 | |
IOS Development Training | May 28 to Jun 12 | |
IOS Development Training | May 30 to Jun 14 |
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.
1 /15
Copyright © 2013 - 2022 MindMajix Technologies