zoukankan      html  css  js  c++  java
  • 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];
    

      

  • 相关阅读:
    剑指 Offer——13. 调整数组顺序使奇数位于偶数前面
    剑指 Offer——3. 从尾到头打印链表
    剑指 Offer——2. 替换空格
    剑指 Offer——1. 二维数组中的查找
    LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
    LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
    SSH 代码笔记
    anaconda3安装caffe
    opencv多版本安装
    人脸文章与数据库
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4411737.html
Copyright © 2011-2022 走看看