Wednesday, April 17, 2013

Simple Table View in IOS

Simple Table View in IOS

Step 1

       Create a Project in xcode 

Step 2
       drag and drop the  Table view in to  ".xib"  .






After Drag & Drop 

Step 3
     
      Click the Table View & click right mouse button .

    set the dataSource & delegate into file owner.




Step 4 
     
 goto the ".h" file of you.



 change the code like this ( add <UITableViewDelegateUITableViewDataSource> )

//--------------------------------------------------------------------------------------------------

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

{
     NSArray *tableData;  //variable for holding the table data
   
}
@property(nonatomic,retain)NSArray *tableData;
@end


Step 5

 goto the ".m" file of you


synthesized the property


      @synthesize tableData;

//-------------------------------------------------


Step 6

add this code to viewDidLoad  method in ".m"

//---------------------------------------------------------------------------------------------------------------------------
  tableData = [NSArray arrayWithObjects:@"SLIIT 1st Year"@"SLIIT 2nd Year"@"SLIIT 3rd Year"@"SLIIT 4th Year",nil];

//---------------------------------------------------------------------------------------------------------------------------


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    tableData = [NSArray arrayWithObjects:@"SLIIT 1st Year", @"SLIIT 2nd Year", @"SLIIT 3rd Year", @"SLIIT 4th Year",nil];
    
}





Step 7

 Add following methods  to ".m"



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
   
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}



Step 8

Run the App











No comments:

Post a Comment