Search This Blog

Oct 11, 2010

UITableView Delegate methods for UITableView Header and Footer.

This example will show delegate methods for UITableView Controller.

1. This delegate method is used for creating number of sections in UITableView.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

2. This delegate method is used for set number of rows in section in UITableView.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 20;

}

3. This delegate method is used for creating or modifying UITableView cell, this method is also used for set customized cell for a UITableView.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UISecondViewTVCell *secondCell = (UISecondViewTVCell *)[tableView dequeueReusableCellWithIdentifier:@"myidentifier"];
if(secondCell == nil){

UIViewController *mycontroller = [[UIViewController alloc]initWithNibName:@"SecondViewTVCell" bundle:nil];
calanderCell = (UISecondViewTVCell *)mycontroller.view;
[mycontroller release];
}
secondCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return secondCell;
}

4. This delegate method is used for creating customized header for a UITableView.
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
viewHeader.backgroundColor = [UIColor colorWithRed:(112.0/255.0) green:(0.0/255.0) blue:(11.0/255.0) alpha:1.0];
UILabel *lblName = [[UILabel alloc]initWithFrame:CGRectMake(87, 5, 50, 21)];
lblName.backgroundColor = [UIColor clearColor];
lblName.textColor = [UIColor whiteColor];
lblName.text = @"Name";
[self.viewHeader addSubview:lblName];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(241, 5, 50, 21)];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.textColor = [UIColor whiteColor];
lblDate.text = @"Date";
[self.viewHeader addSubview:lblDate];
return viewHeader;
}


5. This delegate method is used for getting the selected row index of UITableView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UIViewController *secondView = [[UIViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondView animated:YES];
}


6. This delegate method is used for creating customized header for a UITableView.
-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

if (tableView.tag == 2) {
UIImageView *imgViewFooter = [[UIImageView alloc] initWithFrame:CGRectMake(0 ,200 , 225, 34)];
imgViewFooter.image = [UIImage imageNamed:@"twitterFooter.png"];
return imgViewFooter;
}
return nil;
}

3 comments:

  1. its a really a good info gathered at one place for a glance

    ReplyDelete
  2. how to get section information on selecting a row?

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    ReplyDelete
  3. thank you very much by vinoth ios beginner

    ReplyDelete