• Day11 TableView


    1 tableView 

       1.1设置组

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.carGroups.count;
    }
    

     1.2 设置行

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.cars.count;
    }
    

       1.3 设置内容

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[indexPath.section];
        NSString *name = group.cars[indexPath.row];
        cell.textLabel.text = name;
        return cell;
    }
    

    1.4 设置头部

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.title;
    }
    

    1.5 设置尾部

    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.desc;
    }
    

    1.6 tableView 样式

       1.6.1 UITableViewCellStyleDefault 单行简单样式

    //
    //  ViewController.m
    //  tableViewDemo
    //
    //  Created by xin on 15/4/8.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "carGroup.h"
    
    @interface ViewController ()<UITableViewDataSource>
    
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @property (nonatomic,strong) NSArray *carGroups;
    @end
    
    @implementation ViewController
    
    -(NSArray *)carGroups{
        if(_carGroups == nil){
            carGroup *group1 = [[carGroup alloc]init];
            group1.title = @"德系品牌";
            group1.desc = @"高端大气上档次";
            group1.cars = @[@"奥迪",@"宝马"];
            
            carGroup *group2 = [[carGroup alloc]init];
            group2.title = @"日韩品牌";
            group2.desc = @"牛逼哈哈";
            group2.cars = @[@"本田",@"丰田"];
            
            _carGroups = @[group1,group2];
        }
        
        return _carGroups;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //设置数据源
        self.tableView.dataSource = self;
    }
    
    #pragma mark - UITableViewDataSource
    /*
     * 组
     */
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.carGroups.count;
    }
    
    /*
     * 行
     */
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.cars.count;
    }
    
    /*
     * 设置标题
     */
    
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.title;
    }
    
    /*
     * 设置foot
     */
    
    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[section];
        return group.desc;
    }
    
    /*
     * 告知系统具体的行显示的内容
     */
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        carGroup *group = [[carGroup alloc]init];
        group = self.carGroups[indexPath.section];
        NSString *name = group.cars[indexPath.row];
        cell.textLabel.text = name;
        return cell;
    }
    @end
    

    ps注意点:

    //1
    interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    
    //2 
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.dataSource = self;
        //self.tableView.rowHeight = 60;
        self.tableView.delegate = self;
    }
    

    tableView 的一些属性

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableView.separatorColor = [UIColor redColor];
        self.tableView.separatorColor = [UIColor colorWithRed:199/255.0 green:78/255.0 blue:96/255.0 alpha:1.0f];
        //给整个tableView的头部添加
        self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    

      

  • 相关阅读:
    应对https协议的下载方式
    订单峰值激增 230%,Serverless 如何为世纪联华降本超 40%?|双11 云原生实践
    阿里云函数计算发布新功能,支持容器镜像,加速应用 Serverless 进程
    专访阿里云 Serverless 负责人:无服务器不会让后端失业
    从零入门 Serverless | SAE 的远程调试和云端联调
    我在阿里巴巴做 Serverless 云开发平台
    高德最佳实践:Serverless 规模化落地有哪些价值?
    [图论总结1]最大独立集(例题:Code Names)
    Excel导入SQL工具【3.02】
    学习笔记助手【2.01】base1 更新
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4411737.html
Copyright © 2020-2023  润新知