tableView仿选车软件

#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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值