• 【代码笔记】iOS-3个section,每个都有header.


    一,效果图:

     

    二,工程目录。

     

     

    三,代码

     

    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;
    }
    复制代码

     

     

  • 相关阅读:
    ArrayList和LinkedList的区别
    线程的基本概念、线程的基本状态以及状态之间的关 系
    当一个线程进入一个对象的一个synchronized方法后, 其它线程是否可进入此对象的其它方法?
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.thinkplatform.dao.UserLogDao' available: expected at least 1 bean which qualifies as autowi
    IDEA设置热部署
    spring的核心组件及作用(一)
    解决Linux启动redis时出现提示权限不够问题
    Struts自动装配和四种放入Session作用域的方式
    Struts第一个案例搭建
    当List<String> list =new ArrayList<String>(20); 他会扩容多少次
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/4999683.html
Copyright © 2020-2023  润新知