zoukankan      html  css  js  c++  java
  • 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上了,但是感觉这样会影响运行效率,有时间再优化吧,

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

  • 相关阅读:
    PHP学习笔记:APACHE配置虚拟目录、一个站点使用多域名配置方式
    转载:分页原理+分页代码+分页类制作
    PHP学习笔记:数据库学习心得
    PHP学习笔记:用mysqli连接数据库
    PHP学习笔记:MySQL数据库的操纵
    PHP学习笔记:利用时间和mt_rand函数获取随机名字
    PHP学习笔记:等比例缩放图片
    前端学习(一) html介绍和head标签
    Python 协程
    Python 线程----线程方法,线程事件,线程队列,线程池,GIL锁,协程,Greenlet
  • 原文地址:https://www.cnblogs.com/liuwenqiang/p/5729214.html
Copyright © 2011-2022 走看看