zoukankan      html  css  js  c++  java
  • iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态。刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难。好,不说了,讲正事。

    制作表格,还是需要tableView来做。

    1. 创建一个UIView对象 ;

        UIView *tableViewHeadView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kCount*kWidth, kHeight)];

        self.myHeadView=tableViewHeadView; //(myHeadView 是 UIView)

     2.创建N个字段的View的对象,放到 上面创建的tableViewHeadView上;

        for(int i=0;i<kCount;i++){

            

            UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(i*kWidth, 0, kWidth, kHeight)];

            headView.backgroundColor=[UIColorcolorWithRed:arc4random_uniform(255)/255.0green:arc4random_uniform(255)/255.0blue:arc4random_uniform(255)/255.0alpha:1];

            [tableViewHeadView addSubview:headView];

        }

    3.然后创建一个UITableView对象 ;

        UITableView *tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0, 0, self.myHeadView.frame.size.width, 460) style:UITableViewStylePlain];

        tableView.delegate=self;

        tableView.dataSource=self;

        tableView.bounces=YES;

        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

        self.myTableView=tableView;

        tableView.backgroundColor=[UIColorwhiteColor];

     4.创建一个UIScrollView对象,放置上面的tableView对象,并且将其位置右移部分像素;

        UIScrollView *myScrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(kWidth*0.7, 0, self.view.frame.size.width-kWidth*0.7, 480)];

        [myScrollView addSubview:tableView];

        myScrollView.contentSize=CGSizeMake(self.myHeadView.frame.size.width,0);

        myScrollView.bounces=YES;

        [self.view addSubview:myScrollView];

     5.创建表最左边的的字段的列;//有人会问,为什么不在上面的tableview里面一起表现出来呢。这里,我想告诉你的是,为了实现,表格第一列不动,其他列可以滑动的效果,所以这样做了。这样做 还有最重要一步,就是要实现滚动非第一列的时候,保证整个tableView和 第一列同时滑动,这就是我第6步要实现的了;

        self.timeView=[[TimeView alloc]initWithFrame:CGRectMake(0, 100, kWidth*0.7, kCount*(kHeight+kHeightMargin))]; //在TimeView 类里面,创建了一个tableView

        [self.view addSubview:self.timeView];

    6. 实现UIScrollView的delegate

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        CGFloat offsetY= self.myTableView.contentOffset.y;

        CGPoint timeOffsetY=self.timeView.timeTableView.contentOffset;

        timeOffsetY.y=offsetY;

        self.timeView.timeTableView.contentOffset=timeOffsetY;

        if(offsetY==0){

            self.timeView.timeTableView.contentOffset=CGPointZero;

        }

    }

    7.接下来就是实现tableView的delegate 和 dataSource。

    在这里要说明一下,Mycell这个类的初始化方法里面,又创建了N个view

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

        if (self) {

            

            for(int i=0;i<20;i++){

                UIView *headView=[[UIViewalloc]initWithFrame:CGRectMake(i*kWidth, 0, kWidth-kWidthMargin, kHeight+kHeightMargin)];

                headView.backgroundColor=[UIColor whiteColor];

                [self.contentView addSubview:headView];

            }

            

        }

        returnself;

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return kCount-1;

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *cellIdentifier=@"cell";

        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if(cell==nil){

            

            cell=[[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];

            cell.backgroundColor=[UIColorgrayColor];

    //        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];

        

        }

        return cell;

    }

    8.最后异步需要将前面第1步,创建的那个headerView,放置到表头,并设置表头高度;

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

    {

        

        returnself.myHeadView;

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    {

        

        returnkHeight;

    }

    这里只是写了个极具简单的表格,没有将数据填充进去,后续会加进去的。。。

  • 相关阅读:
    RocketMQ(4.8.0)——Broker读写分离机制
    RocketMQ(4.8.0)——Broker消息存储机制
    RocketMQ(4.8.0)——Broker 概述、启动和停止流程
    RocketMQ(4.8.0)——RocketMQ的路由原理
    RocketMQ(4.8.0)——Namesrv 服务
    RocketMQ(4.8.0)——RocketMQ部署拓扑和部署实践
    RocketMQ(4.8.0)——RocketMQ体系架构
    RocketMQ(4.8.0)——消费者最佳实践
    Java丨时间判断谁前谁后
    MySQL丨分页查询
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3782602.html
Copyright © 2011-2022 走看看