iOS 制作表格 (数据源控制行,列数)

时间:2023-03-09 09:56:34
iOS 制作表格 (数据源控制行,列数)

  记得去年面试的过程中,有一个面试官问我怎么制作表格。由于之前也没有做过,当时有点懵逼,今天想起来了,就用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(, , WQwidth, WQheight);
scrollview.backgroundColor =[ UIColor grayColor];
scrollview.contentSize = CGSizeMake(WQwidth, WQheight+); scrollview.delegate =self;
self.scrollview =scrollview;
[self.view addSubview:scrollview]; for (int i=; i<; i++) { UITableView *tableview =[[UITableView alloc]init];
tableview.frame = CGRectMake(i*WQwidth/+0.5, , (WQwidth-*0.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 ; }
-(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:];
cell.separatorInset = UIEdgeInsetsMake(,- , , );
} return cell; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView.tag ==||tableView.tag ==) {
return @"图书";
}else{ return nil;
} } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{ if (tableView.tag ==||tableView.tag ==) {
return WQheight/;
}else{
return ;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return WQheight/; }
-(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上了,但是感觉这样会影响运行效率,有时间再优化吧,

大家有什么好的意见给我说一下,谢谢啦