標籤:
記得去年面試的過程中,有一個面試官問我怎麼製作表格。由於之前也沒有做過,當時有點懵逼,今天想起來了,就用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 製作表格 (資料來源控制行,列數)