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(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上了,但是感觉这样会影响运行效率,有时间再优化吧,

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

时间: 2024-07-29 16:15:46

iOS 制作表格 (数据源控制行,列数)的相关文章

1027 制作表格

表格分 : 行   列 分三个标签: <table>(开始) <tr> <td> </td> </tr> </table>(结束) 表格宽度百分百相对浏览器边框  1-100% 单元格边距:边线对边线的距离 单元格间距:内容与边线的距离 width(宽度)="100%" border(边框)="1" cellspacing(边距)="0" cellpadding(间距)=&q

freemarker导出word——让表格数据行数 列数自动变化

行数.列数变化只需定义一个List<List<T>> freemarker遍历的话,只需要使用freemarker的标记性语言<#list report.qc_third_agentTable as  table2_tr>遍历即可,如图 实现的效果 freemarker导出word--让表格数据行数 列数自动变化,布布扣,bubuko.com

VBA取得EXCEL表格中的行数和列数

VBA取得EXCEL表格中的行数和列数 初学EXCEL宏的童鞋,总是很想知道表格中含有数据的行数和列数,尤其是行数和列数不确定的情况下.这样可以避免很多的错误,并且可以提高效率.但每次用到的时候到网上查找时,总是给了很多无用的答案,往往找不到想要的结果.笔者也是每次使用时,临时查找总是很头疼.偶然发现一篇博客,上面详细记录了不同的方法,笔者测试了几种发现真的很好用.本着分享万岁的精神,将博客内容共享出来.希望对大家有所帮助. 来源:http://www.okexcel.com.cn/bbs/vi

用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法

用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count ActiveSheet.UsedRange.Columns.Count 缺点:有时可能会比实际数大一些,原因是如果你把最后几行(列)数据清除后(非整行或整列删除), 用这个命令仍返回未清除前的值.就是说现在虽然是空的,但是你曾经用过也算你的. 方法2: ActiveSheet.Range("A65535

POI获取Excel列数和行数的方法

//获取指定行,索引从0开始 hssfRow=hssfSheet.getRow(1); //获取指定列,索引从0开始 hssfCell=hssfRow.getCell((short)6); //获取总行数 //int rowNum=hssfSheet.getLastRowNum(); //获取一个excel表格中的总记录数 int rowNum=storagesList.size(); //获取总列数 int columnNum=hssfRow.getPhysicalNumberOfCells(

JAVA使用POI获取Excel的列数与行数

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 下面这篇文章给大家介绍了JAVA使用POI获取Excel列数和行数的方法,有需要的朋友们可以参考借鉴,下面来一起看看吧. 前言 报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用性,不方便用户进行个性化编辑.Java程序由于其跨平台特性,不能直接操纵Excel.因此,本文探讨一下POI视线Java程序

C#中如何获取一个二维数组的两维长度,即行数和列数?以及多维数组各个维度的长度?

如何获取二维数组中的元素个数呢? int[,] array = new int[,] {{1,2,3},{4,5,6},{7,8,9}};//定义一个3行3列的二维数组int row = array.Rank;//获取维数,这里指行数int col = array.GetLength(1);//获取指定维度中的元素个数,这里也就是列数了.(0是第一维,1表示的是第二维)int col = array.GetUpperBound(0)+1;//获取指定维度的索引上限,在加上一个1就是总数,这里表示

如何获取ResultSet的行数和列数

当我们执行数据库查询返回一个ResultSet的时候,很多情况下我们需要知道这个ResultSet的大小,即它的行数和列数.我们知道它的列数可以通过resultSet.getMetaData().getColumnCount()很容易地得到,然而,java API没有提供直接访问ResultSet行数的接口. 这个时候,有三个办法可以解决: 1.改用select count语句,然后直接从ResultSet里面获取结果: try { Statement statement = connectio

c语言:实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定

实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表. 程序: #include<stdio.h> void mul(int n)//multiplication 乘法 { int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%d*%d=%-2d  ", i, j, i*j); //其中%2d中的2表示