Powered By Blogger

Tuesday, April 17, 2012

How to create UITableView in iOS SDK ?

Hello friends ,
   I'm going to explain you how to create table view by programmatically  in iOS application. Please follow the below code and be sure to include/ set its delegate and datasource property. You have to set "UITableViewDataSource" and "UITableViewDelegate" into the .h[header section] of a class.

UITableView *tblView = [[UITableView alloc]init];
tblView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height); // here , you can set you tableview's frame in view.
tblView.delegate = self; // for delegate methods this one need to set compulsary
tblView.dataSource = self;// for datasource methods this one need to set compulsary
tblView.tag = 3; // Tag is used to make the seperation between multiple tables.
[self.view addSubview:tblView]; // place tableview on the main view on which you want to display it.

@optional  // Optional method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;             
{
    return numberOfsection; // Default is 1 if not implemented
}

@required  // required datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberOfRowsInEachSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell=[tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
        cell.textLabel.text = @"Hello"; // You can place you content string here what you want to show in each row of the tableview

    return cell;
}

@required // Required delegate methods

- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *viewController = [[DetailViewController alloc]initWithNibName:@"DetailView" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}

Hope, by this you may get idea about to create a table-view pro-grammatically in an application.



Thanks and Regards,
Nilesh Prajapati
 

No comments:

Post a Comment