Header and footer of UITableView in IOS development (26)

Source: Internet
Author: User

1 Preface
In each Section of UITableView, you can set the header and footer to meet your needs. You can set it by yourself.

2. code example
ZYViewHeaderFooterController. h:

 

[Plain]
# Import <UIKit/UIKit. h>
 
@ Interface ZYViewHeaderFooterController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
 
@ Property (nonatomic, strong) UITableView * myTableView;
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ZYViewHeaderFooterController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy

@ Property (nonatomic, strong) UITableView * myTableView;

@ End
ZYViewHeaderFooterController. m:

 

[Plain]
@ Synthesize myTableView;
 
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Return self;
}
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];
}
 
// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 3;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}
// Set the Section Header
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {
NSString * result = nil;
If ([tableView isEqual: myTableView] & § ion = 0 ){
Result = @ "Section 0 Header ";
}
Return result;
}
// Set Footer of Section
-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {
NSString * result = nil;
If ([tableView isEqual: myTableView] & § ion = 0 ){
Result = @ "Section 0 Header ";
}
Return result;
}

@ Synthesize myTableView;

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Return self;
}

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];
}

// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 3;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}
// Set the Section Header
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {
NSString * result = nil;
If ([tableView isEqual: myTableView] & § ion = 0 ){
Result = @ "Section 0 Header ";
}
Return result;
}
// Set Footer of Section
-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {
NSString * result = nil;
If ([tableView isEqual: myTableView] & § ion = 0 ){
Result = @ "Section 0 Header ";
}
Return result;
}
Running result:

 
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.