【代碼筆記】3個section,每個都有header.,sectionheader

來源:互聯網
上載者:User

【代碼筆記】3個section,每個都有header.,sectionheader

一,:

 

二,工程目錄。

 

 

三,代碼

 

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 *)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.        //更新背景圖    [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 dequeueReusableCellWithIdentifier:@"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=@"電影";    }else if(section==1)    {        label.text=@"電視劇";    }else if(section==2)    {        label.text=@"動漫";    }    [view addSubview:label];    return  view;}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.