ios-day06-01(UITableView的使用:分组、设置数据源、UITableViewDataSource协议等)

本文介绍了一个用于展示汽车品牌的iOS应用程序。该应用使用UITableView来显示不同汽车品牌及其车型,并通过plist文件加载数据。实现了UITableViewDataSource协议以填充表格视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8443347

效果图:

         


核心代码:

//
//  LiuJieCarGroup.h
//  01-汽车品牌
//
//  Created by XinYou on 15-2-12.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
   
    

@interface LiuJieCarGroup : NSObject
/**
 *  头部标题
 */
@property (nonatomic, copy) NSString *title;
/**
 *  尾部描述
 */
@property (nonatomic, copy) NSString *desc;
/**
 *  每组的汽车名集合
 */
@property (nonatomic, strong) NSArray *cars;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)carGroupWithDict:(NSDictionary *)dict;

@end
//
//  LiuJieCarGroup.m
//  01-汽车品牌
//
//  Created by XinYou on 15-2-12.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieCarGroup.h"

@implementation LiuJieCarGroup

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        self.title = dict[@"title"];
        self.desc = dict[@"desc"];
        self.cars = dict[@"cars"];
    }
    
    return self;
}


+ (instancetype)carGroupWithDict:(NSDictionary *)dict{
    
    return [[self alloc] initWithDict:dict];
}
@end
//
//  LiuJieViewController.m
//  01-汽车品牌
//
//  Created by XinYou on 15-2-12.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"
#import "LiuJieCarGroup.h"

@interface LiuJieViewController () 
    
     
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;

@end

@implementation LiuJieViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置UITableView的数据源,数据源需要实现UITableViewDataSource协议
    self.tableView.dataSource = self;
    
}

/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden{

    return YES;
}

- (NSArray *)carGroups{

    if (_carGroups == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_simple.plist" ofType:nil];
        
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *tempCarGroups = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            LiuJieCarGroup *carGroup = [LiuJieCarGroup carGroupWithDict:dict];
            
            [tempCarGroups addObject:carGroup];
        }
        
        _carGroups = tempCarGroups;
    }
    
    return _carGroups;
}
/**
 *  @return 一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.carGroups.count;
}
/**
 *  @return 每组数据各有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    LiuJieCarGroup *carGroup = self.carGroups[section];
    
    NSInteger rows = carGroup.cars.count;
    
    return rows;
}
/**
 *  @return 每行数据对应的UITableViewCell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
//    NSInteger section = indexPath.section;
//    NSInteger row = indexPath.row;
    
    LiuJieCarGroup *carGroup = self.carGroups[indexPath.section];
    
    cell.textLabel.text = carGroup.cars[indexPath.row];
    
    return cell;
}
/**
 *  @return 某一个组的顶部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    LiuJieCarGroup *carGroup = self.carGroups[section];
    
    NSString *title = carGroup.title;
    
    return title;
}
/**
 *  @return 某一个组的底部标题(描述)
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    LiuJieCarGroup *carGroup = self.carGroups[section];
    
    return carGroup.desc;
}

@end

    
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值