可不写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