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: