iOS 製作表格 (資料來源控制行,列數)

來源:互聯網
上載者:User

標籤:

  記得去年面試的過程中,有一個面試官問我怎麼製作表格。由於之前也沒有做過,當時有點懵逼,今天想起來了,就用tableview製作了一個,望不要有人像我一樣掉坑了,

直接上代碼:

////  ViewController.m//  表格-test////  Created by abc on 16/8/2.//  Copyright © 2016年 LiuWenqiang. All rights reserved.//#import "ViewController.h"#define WQwidth  [UIScreen mainScreen].bounds.size.width#define WQheight  [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>@property(strong,nonatomic) UITableView * tableview;@property(strong,nonatomic) UIScrollView * scrollview;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor = [UIColor grayColor];        UIScrollView *scrollview =[[ UIScrollView alloc]init];    scrollview.frame = CGRectMake(0, 20, WQwidth, WQheight);    scrollview.backgroundColor =[ UIColor grayColor];    scrollview.contentSize = CGSizeMake(WQwidth, WQheight+50);    scrollview.delegate =self;    self.scrollview =scrollview;    [self.view addSubview:scrollview];            for (int i=0; i<5; i++) {                UITableView *tableview =[[UITableView alloc]init];        tableview.frame = CGRectMake(i*WQwidth/5+0.5, 0, (WQwidth-6*0.5)/5, scrollview.contentSize.height);        tableview.backgroundColor = [UIColor whiteColor];        tableview.delegate =self;        tableview.dataSource = self;        tableview.scrollEnabled = NO;        tableview.tag = i;        self.tableview =tableview;        self.tableview.tableFooterView = [[UIView  alloc]init];        [self.scrollview addSubview:tableview];            }    }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 30;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];        if (!cell) {        cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];        cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",(long)indexPath.row];        cell.textLabel.font = [UIFont systemFontOfSize:13];        cell.separatorInset = UIEdgeInsetsMake(0,-20 , 0, 0);    }        return cell;}-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (tableView.tag ==2||tableView.tag ==1) {        return @"圖書";    }else{            return  nil;    }}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{       if (tableView.tag ==2||tableView.tag ==1) {    return WQheight/30;       }else{           return 0;       }}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return WQheight/30;    }-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:YES];        NSLog(@"---------(%ld,%ld)",(long)tableView.tag,(long)indexPath.row);    }-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{            if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {            [cell setSeparatorInset:UIEdgeInsetsZero];        }        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {            [cell setLayoutMargins:UIEdgeInsetsZero];        }        //按照作者最後的意思還要加上下面這一段        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){            [cell setPreservesSuperviewLayoutMargins:NO];        }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

由於表格一般都是整體滾動的,就關閉了tableview的滾動,把tableview就放到uiscrollview上了,但是感覺這樣會影響運行效率,有時間再最佳化吧,

大家有什麼好的意見給我說一下,謝謝啦

 

iOS 製作表格 (資料來源控制行,列數)

聯繫我們

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