#import "ViewController.h"
#import "Dict.h"
#import "Name.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property(nonatomic,strong)NSArray *carData;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.separatorColor = [UIColor greenColor];
}
// 隐藏状态栏的方法
- (BOOL)prefersStatusBarHidden
{
return YES;
}
// 懒加载
- (NSArray *)carData
{
if (_carData == nil) {
// 获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_simple.plist" ofType:nil];
// 利用文件路径加载文件到数组
NSArray *names = [NSArray arrayWithContentsOfFile:path];
// 定义一个可变数组保存模型
NSMutableArray *nameM = [NSMutableArray array];
// 利用For循环遍历数组并且完成数据转模型
for (NSDictionary *dict in names) {
Dict *carM = [Dict dictWithDict:dict];
[nameM addObject:carM];
}
self.carData = nameM;
}
return _carData;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Dict *carM = self.carData[section];
return carM.names.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 设置标识
static NSString *ID = @"car";
// 在缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果缓存池中没有cell,就创建一个新的cell,并且贴上标识
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 给cell设置数据
Dict *carM = self.carData[indexPath.section];
Name *str = carM.names[indexPath.row];
cell.textLabel.text = str.carName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor =[UIColor brownColor];
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = view;
return cell;
}
// cell的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Dict *carM = self.carData[section];
return carM.title;
}
// cell的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
Dict *carM = self.carData[section];
return carM.desc;
}
int num;
// 当用户点击cell时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"A" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
Dict *carM = self.carData[indexPath.section];
Name *str = carM.names[indexPath.row];
[alert textFieldAtIndex:0].text = str.carName;
alert.tag = indexPath.row;
num = indexPath.section;
[alert show];
}
// 更改数据并刷新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) return;
// 1、取得文本框最后的文字
NSString *name = [alertView textFieldAtIndex:0].text;
// 2、修改模型数据
int row = alertView.tag;
Dict *carM = self.carData[num];
Name *str = carM.names[row];
str.carName = name;
// 3、刷新数据
// [self.tableView reloadData];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:num];
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];
}
@end