ViewController UITableView

本文介绍了一个iOS应用程序如何从plist文件加载汽车品牌及相关信息,并展示这些数据在一个UITableView中。具体步骤包括读取文件、解析数据并将其转换为可使用的模型对象。

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

//

//  ViewController.m

//  CarDemo

//

//  Created by GuoYule on 15/1/20.

//  Copyright (c) 2015 GuoYule. All rights reserved.

//


#import "ViewController.h"

#import "CarGroup.h"

#import "GYLcar.h"

@interface ViewController ()<UITableViewDataSource>

/**

 *  车品牌数据

 */

@property(nonatomic,strong) NSArray * carGroups;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


-(BOOL)prefersStatusBarHidden

{

    return YES;

}

-(NSArray *)carGroups


{

    if (_carGroups == nil) {

        // 初始化

        

        // 1.获得plist的全路径

        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];

        

        // 2.加载数组

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

        

        // 3.dictArray里面的所有字典转成模型对象,放到新的数组中

        NSMutableArray *carGroupsArray = [NSMutableArray array];

        for (NSDictionary *dict in dictArray) {

            // 3.1.创建模型对象

            CarGroup *group = [CarGroup groupWithDict:dict];

            

            // 3.2.添加模型对象到数组中

            [carGroupsArray addObject:group];

        }

        

        // 4.赋值

        _carGroups = carGroupsArray;

    }

    return _carGroups;

    

    

}

#pragma mark -数据源模型

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.carGroups.count;

    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    CarGroup * group = self.carGroups[section];

    return group.cars.count;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //定义一个循环标识

    static NSString * ID = @"car";

    //2.从缓存池中取出可循环利用的cell

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];

    //3. 如果缓存池中没有那么就要新建一个 并且要赋予标识

    if (cell == nil)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    //4.设置数据

    CarGroup * group = self.carGroups[indexPath.section];

    GYLcar * car = group.cars[indexPath.row];

    cell.imageView.image = [UIImage imageNamed:car.icon];

    cell.textLabel.text = car.name;

    return  cell;

}

/**

 *  section组显示的头部标题


 */

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    CarGroup * group = self.carGroups[section];

    return group.title;

}

/**

 *  返回回右边索引跳显示的字符串数据

 */

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return [self.carGroups valueForKeyPath:@"title"]; //这是KVC可以间接取值

    //为什么不用[self.carGroups valueForKey:@"title"] 因为这个不会间接取值

    

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值