标签:
记得去年面试的过程中,有一个面试官问我怎么制作表格。由于之前也没有做过,当时有点懵逼,今天想起来了,就用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上了,但是感觉这样会影响运行效率,有时间再优化吧,
大家有什么好的意见给我说一下,谢谢啦
标签:
原文地址:http://www.cnblogs.com/liuwenqiang/p/5729214.html