[Code Note] three sections, each with header., sectionheader
I ,:
2. project directory.
Iii. Code
RootViewController. h
#import <UIKit/UIKit.h>@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ UITableView *MyTableView;}@end
RootViewController. m
# Import "RootViewController. h "@ interface RootViewController () @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // update the background image [self initBackGroundView];} # pragma-mark-functions-(void) initBackGroundView {// tableView MyTableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 50, self. view. frame. size. width, 400) style: UITableViewStylePlain]; MyTableView. delegate = self; MyTableView. dataSource = self; [self. view addSubview: MyTableView];} # pragma-mark-UITableViewDelegate-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 1;}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 105;}-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {return 30 ;} -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 3;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView identifier: @ "ID"]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @ "ID"];} if (indexPath. section = 0) {cell. textLabel. text = @ "0"; cell. backgroundColor = [UIColor greenColor];} else if (indexPath. section = 1) {cell. textLabel. text = @ "1"; cell. backgroundColor = [UIColor redColor];} else if (indexPath. section = 2) {cell. textLabel. text = @ "2"; cell. backgroundColor = [UIColor orangeColor];} return cell;}-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section {UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, self. view. frame. size. width, 30)]; view. backgroundColor = [UIColor blackColor]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (10, 0,100, 30)]; label. backgroundColor = [UIColor clearColor]; label. textColor = [UIColor whiteColor]; if (section = 0) {label. text = @ "movie";} else if (section = 1) {label. text = @ "TV series";} else if (section = 2) {label. text = @ "Anime";} [view addSubview: label]; return view ;}