UITableView + 汽车品牌 初始用

1,单组,也就是只有一个section
可不写numberOfSectionsInTableView方法,默认为1组

2,多组,有多个section,风格(storeboard 选择TableView后,在IDE的style中设置)都有两种:plain / grouped;grouped在不同组之间会有间距



3,遵守UITableViewDatasource协议,并指定UITableView的datasource来源,还可以遵守UITableViewDelegate协议后,实现其他代理方法


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 
}

// 心得体会:如果这里错误返回为0,那么cellForRowAtIndexPath永远不会被执行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MJCarGroup *group = self.groups[section];
    
    return group.cars.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从缓存池中取出可循环利用cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 缓存池中没有可循环利用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
}


/**
 *  第section组显示的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{


}


/**
 *  返回右边索引条显示的字符串数据
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{


}


- (BOOL)prefersStatusBarHidden
{
    return YES;
}


4,展示汽车品牌(多组)+ MVC + 性能优化(重复使用已经产生的cell对象)
M: 将PLIST读取后的Dictionary字段转换为数据对象模型
V:在C中一起产生

C: 产生UI,并将模型数据填充到CELL中,并最终显示


(在敲代码时,对KVC不怎么理解,其次建议能拷贝就拷贝,错误把"cars"写成了"car",导致化了很多时间追踪信息不能显示的问题,字典转模型也要注意一些,不会自觉使用self alloc,而还是死办法用 类 alloc方式;还有类方法允许 创建类对象,但是对象方法不应该再去创建对象,那么不就是死循环了吗??)


Style: plain   (同一section内,往上滚动时,header文字保持不变)



Style:  grouped   (同一section内,往上滚动时,header文字不会保持)




#import <Foundation/Foundation.h>


@interface TXCar : NSObject


@property (nonatomic, copy) NSString *icon;


@property (nonatomic, copy) NSString *name;


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


@end

========

#import "TXCar.h"


@implementation TXCar


+ (instancetype)carWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if(self = [super init]){
        [self setValuesForKeysWithDictionary:dict];
        //self.icon = dict[@"icon"];
        //self.name = dict[@"name"];
    }
    return self;
}


@end

=======

#import <Foundation/Foundation.h>


@interface TXCarGroup : NSObject


@property (nonatomic, copy) NSString *title;


@property (nonatomic, strong) NSArray *cars;


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


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


@end

==========

#import "TXCarGroup.h"
#import "TXCar.h"


@implementation TXCarGroup


+ (instancetype)carGroupWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}


- (instancetype)initWithDict:(NSDictionary *)dict
{
    if(self = [super init]){
        self.title = dict[@"title"];
        NSArray *dictArray = dict[@"cars"];
        
        NSMutableArray *carArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            TXCar *car = [TXCar carWithDict:dict];
            [carArray addObject:car];
        }
        self.cars = carArray;
    }
    return self;
}




@end

=============

#import "TXViewController.h"
#import "TXCar.h"
#import "TXCarGroup.h"


@interface TXViewController () <UITableViewDataSource>
@property (nonatomic, strong) NSArray *carGroups;
@end


@implementation TXViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //NSLog(@"----1-----");
    return self.carGroups.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    TXCarGroup *group = self.carGroups[section];
    //NSLog(@"-%d---%d-----",section, group.cars.count);
    return group.cars.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"----3-----");
    static NSString *ID = @"cars";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    TXCarGroup *group = self.carGroups[indexPath.section];
    
    TXCar *car = group.cars[indexPath.row];
    
    cell.textLabel.text = car.name;
    
    cell.imageView.image = [UIImage imageNamed:car.icon];
    
    //NSLog(@"%@--%@", car.name, car.icon);
    
    return cell;
    
}


- (BOOL)prefersStatusBarHidden
{
    return YES;
}


- (NSArray *)carGroups
{
    if(_carGroups == nil)
    {
        NSMutableArray *tmpArr = [NSMutableArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
        NSArray *carArray = [NSArray arrayWithContentsOfFile:path];
        
        for (NSDictionary *dict in carArray) {
            TXCarGroup *cargroup = [TXCarGroup carGroupWithDict:dict];
            [tmpArr addObject:cargroup];
            
            //NSLog(@"=%@==%@",dict[@"cars"],dict[@"title"]);
        }
        _carGroups = tmpArr;
    }
    //NSLog(@"%@", _carGroups[0]);
    
    return _carGroups;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    TXCarGroup *group = self.carGroups[section];
    return group.title;
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.carGroups valueForKeyPath:@"title"];
}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值