iOS表格製作

來源:互聯網
上載者:User

標籤:style   color   width   資料   string   set   

由於項目上的需求,需要做一個表格出來,來顯示流程狀態。剛開始腦子一頭霧水,沒有一點思路,但是靠著自己的座右銘--“世上無難事,只怕有心人”,克服了所有困難。好,不說了,講正事。

製作表格,還是需要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;

}

 

這裡只是寫了個極具簡單的表格,沒有將資料填充進去,後續會加進去的。。。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.