标签:
1.遵守协议<UITableViewDataSource>
2.设置数据源:
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源
self.tableView.dataSource = self;
}
3.必须实现两个方法
1>返回每一组的行数,第2个参数就是表示第几组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
2>返回每一组每一行的内容,NSIdexPath有两个属性,分别是section和row.表示组合列
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
4.简单代码示例(显示汽车品牌)
1>控制器代码
#import "ViewController.h"
#import "WDJGropCars.h"
#import "WDJCars.h"
@interface ViewController ()<UITableViewDataSource>
@property (nonatomic, strong) NSArray *gropArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源
self.tableView.dataSource = self;
}
#pragma mark -
#pragma mark 返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.gropArr.count;
}
#pragma mark -
#pragma mark 返回每一组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
WDJGropCars *gropCar = self.gropArr[section];
return gropCar.cars.count;
}
#pragma mark -
#pragma mark 设置每一行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 定义标示
static NSString *ID = @"car";
// 获取缓存池中对应标示的对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果在缓存池中没有找到,就手动创建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 设置cell的内容
WDJGropCars *gropCar = self.gropArr[indexPath.section];
WDJCars *car = gropCar.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
#pragma mark -
#pragma mark 设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
WDJGropCars *gropCar = self.gropArr[section];
return gropCar.title;
}
#pragma mark -
#pragma mark 设置索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.gropArr valueForKey:@"title"];
}
#pragma mark -
#pragma mark 懒加载
- (NSArray *)gropArr {
if (_gropArr == nil) {
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
NSMutableArray *Marr = [NSMutableArray arrayWithCapacity:arr.count];
for (NSDictionary *dict in arr) {
WDJGropCars *gropCars = [WDJGropCars gropCarsWithDict:dict];
[Marr addObject:gropCars];
}
_gropArr = Marr;
}
return _gropArr;
}
@end
2>车组的模型
1)声明
#import <Foundation/Foundation.h>
@interface WDJGropCars : NSObject
/**
* 车数组
*/
@property (nonatomic, strong) NSArray *cars;
/**
* 标题
*/
@property (nonatomic, copy) NSString *title;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)gropCarsWithDict:(NSDictionary *)dict;
@end
2)实现
@implementation WDJGropCars
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
NSArray *carArr = dict[@"cars"];
NSMutableArray *Marr = [NSMutableArray arrayWithCapacity:carArr.count];
for (NSDictionary *carDict in carArr) {
WDJCars *cars = [WDJCars carsWithDict:carDict];
[Marr addObject:cars];
}
self.cars = Marr;
self.title = dict[@"title"];
}
return self;
}
+ (instancetype)gropCarsWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end
3>车的模型
1)声明
@interface WDJCars : NSObject
/**
* 车的图片名称
*/
@property (nonatomic, copy) NSString *icon;
/**
* 车的名称
*/
@property (nonatomic, copy) NSString *name;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carsWithDict:(NSDictionary *)dict;
@end
2)实现
#import "WDJCars.h"
@implementation WDJCars
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)carsWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end
标签:
原文地址:http://www.cnblogs.com/wangdjblogs/p/5513836.html