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];
    

      

  • 相关阅读:
    在变量中如何插入变量
    perl 模块
    perl中的引用
    数组:pop&清空数组&查找某元素是否在数组内
    整个文件做为一个数组
    checkbox判断选中
    网页存储倒计时与解决网页cookie保存多个相同key问题
    wmframework v2.0 手册(一)系统框架介绍
    r cannot be resolved to a variable android
    锁定Chrome的下载文件夹快捷方式到win7任务栏
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4411737.html
Copyright © 2011-2022 走看看