您的位置:首页 > 其它

设置tableviewView组的快速检索

2016-04-24 17:17 309 查看
@interface
ViewController ()<UITableViewDataSource>

@property (nonatomic,
strong) NSArray *dataArray;

@property (nonatomic,
strong) NSArray *indexArray;//检索的索引数组


@end

@implementation ViewController

#pragma mark -

#pragma mark - 懒加载

- (NSArray *)dataArray {

if (nil ==
_dataArray) {

// 1. 路径

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

// 2. 读取

NSArray *tempArray = [NSArray
arrayWithContentsOfFile:path];

// 3.
临时可变数组

NSMutableArray *mutal = [NSMutableArray
array];

// 4. 转

for (NSDictionary *dict
in tempArray) {

CarModel *model = [CarModel
carModelWithDict: dict];

[mutal addObject:model];

}

//
把carModel中title
放入数组,
用作后面的索引返回值


_indexArray = [mutal
valueForKeyPath:@"title"];


_dataArray = mutal;

}

return
_dataArray;

}

- (void)viewDidLoad {

[super
viewDidLoad];

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

}

// 多少组

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

return
self.dataArray.count;

}

// 多少行

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

CarModel *carModel =
self.dataArray[section];

return carModel.cars.count;

}

// 每一行显示的内容

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

// 1.
定义一个重用标识符

static NSString *identifier =
@"settingCell";

// 2. 到缓存池中去找对应的cell,
根据重用标识符

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identifier];

// 3. 判断是否找到,
如果找不到, 就重新实例化cell

if (nil == cell) {

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

}

// 4.
设置数据

//
先取出该组对应的model

CarModel *carModel =
self.dataArray[indexPath.section];

//
再取出该组中行对应的model

InnerCarModel *innerCarModel = carModel.cars[indexPath.row];

//
对cell的控件进行赋值

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

cell.textLabel.text = innerCarModel.name;

return cell;

}

//设置组头标题

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

//
取出carModel

CarModel *carModel =
self.dataArray[section];

return carModel.title;

}

//设置组的快速搜索

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

return
_indexArray;


}

//隐藏状态栏

- (BOOL)prefersStatusBarHidden {

return
YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: