zoukankan      html  css  js  c++  java
  • 源码-0204-UITableView02

    //
    //  ViewController.m
    //  05-UITableView01-多组数据
    #import "ViewController.h"
    #import "XMGCar.h"
    #import "XMGCarGroup.h"
    
    @interface ViewController () <UITableViewDataSource>
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    /** 所有组数据 */
    @property (nonatomic, strong) NSArray *groups;
    @end
    
    @implementation ViewController
    
    - (NSArray *)groups
    {
        if (_groups == nil) {
            // 创建模型数据
            XMGCarGroup *group0 = [[XMGCarGroup alloc] init];
            group0.header = @"德系";
            group0.footer = @"德系车666";
            group0.cars = @[
                            [XMGCar carWithName:@"奥迪" icon:@"m_9_100"],
                            [XMGCar carWithName:@"宝马" icon:@"m_3_100"],
                            [XMGCar carWithName:@"奔驰" icon:@"m_2_100"]
                            ];
            
            XMGCarGroup *group1 = [[XMGCarGroup alloc] init];
            group1.header = @"日系";
            group1.footer = @"日系车666777";
            group1.cars = @[
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
                            [XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰1" icon:@"m_2_100"]
                            ];
            
            XMGCarGroup *group2 = [[XMGCarGroup alloc] init];
            group2.header = @"日系1";
            group2.footer = @"日系车999999";
            group2.cars = @[
                            [XMGCar carWithName:@"奥迪2" icon:@"m_9_100"],
                            [XMGCar carWithName:@"奔驰2" icon:@"m_2_100"]
                            ];
            
            _groups = @[group0, group1, group2];
        }
        return _groups;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 设置数据源
        self.tableView.dataSource = self;
    }
    
    #pragma mark - <UITableViewDataSource>
    /**
     *  告诉tableView第section组有多少行
     */
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        XMGCarGroup *group = self.groups[section];
        return group.cars.count;
    }
    
    /**
     *  告诉tableView一共有多少组数据
     */
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return self.groups.count;
    }
    
    /**
     *  告诉tableView第indexPath行显示怎样的cell
     */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        
        XMGCarGroup *group = self.groups[indexPath.section];
        XMGCar *car = group.cars[indexPath.row];
        
        cell.textLabel.text = car.name;
        cell.imageView.image = [UIImage imageNamed:car.icon];
        
        return cell;
    }
    
    /**
     *  告诉tableView第section组的头部标题
     */
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        XMGCarGroup *group = self.groups[section];
        return group.header;
    }
    
    /**
     *  告诉tableView第section组的尾部标题
     */
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    {
        XMGCarGroup *group = self.groups[section];
        return group.footer;
    }
    @end
    //
    //  XMGCar.h
    //  05-UITableView01-多组数据
    #import <Foundation/Foundation.h>
    
    @interface XMGCar : NSObject
    /** 名字 */
    @property (nonatomic, strong) NSString *name;
    /** 图标 */
    @property (nonatomic, strong) NSString *icon;
    
    + (instancetype)carWithName:(NSString *)name icon:(NSString *)icon;
    @end
    //
    //  XMGCar.m
    //  05-UITableView01-多组数据
    #import "XMGCar.h"
    
    @implementation XMGCar
    + (instancetype)carWithName:(NSString *)name icon:(NSString *)icon
    {
        XMGCar *car = [[self alloc] init];
        car.name = name;
        car.icon = icon;
        return car;
    }
    @end
    //
    //  XMGCarGroup.h
    //  05-UITableView01-多组数据
    #import <Foundation/Foundation.h>
    
    @interface XMGCarGroup : NSObject
    /** 头部标题 */
    @property (nonatomic, strong) NSString *header;
    /** 尾部标题 */
    @property (nonatomic, strong) NSString *footer;
    /** 这组所有的车辆模型(这个数组里面存放的都是XMGCar模型) */
    @property (nonatomic, strong) NSArray *cars;
    @end
    //
    //  XMGCarGroup.m
    //  05-UITableView01-多组数据
    #import "XMGCarGroup.h"
    
    @implementation XMGCarGroup
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    C# 之 读取Word时发生 “拒绝访问” 及 “消息筛选器显示应用程序正在使用中” 异常的处理
    sctp和tcp的区别
    kmalloc/kfree,vmalloc/vfree函数用法和区别
    C语言中volatile关键字的作用
    嵌入式开发之NorFlash 和NandFlash
    linux中断--中断嵌套&中断请求丢失
    Linux 套接字编程中的 5 个隐患
    socket编程中write、read和send、recv之间的区别
    HTTP/1.1 Range和Content-Range
    探讨read的返回值的三种情况
  • 原文地址:https://www.cnblogs.com/laugh/p/6428138.html
Copyright © 2011-2022 走看看