14

@interface ViewController () <AddViewControllerDelegate>

 

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArray;

 

- (void) handleData;

@end

 

@implementation ViewController

- (void)sendStudent:(Student *)student {

    [_dataArray insertObject:student atIndex:0];

    

    //重走数据源协议方法

    [self.tableView reloadData];

}

 

 

-(void) handleData {

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];

    //从文件中提取数组

    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];

    for (NSDictionary *dic in array) {

        Student *student = [[Student alloc]init];

        [student setValuesForKeysWithDictionary:dic];

        [_dataArray addObject:student];

        

    }

}

 

 

- (void)add {

    AddViewController *addVC = [[AddViewController alloc] init];

    [self.navigationController pushViewController:addVC animated:YES];

    addVC.delegate = self;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

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

    self.navigationController.title = @"通讯录";

    self.view.backgroundColor = [UIColor whiteColor];

    [self handleData];

    

    UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];

    self.navigationItem.leftBarButtonItem = addItem;

    

    

    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

    

    _tableView.dataSource = self;

    _tableView.delegate = self;

    

    [self.view addSubview:_tableView];

}

 

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

    return  _dataArray.count;

}

 

 

 

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

    

    static NSString *reuseIdentifier = @"reuse";

 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    

    if (cell == nil) {

         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];

    }

    

//    cell.selectionStyle = UITableViewSelectionDidChangeNotification

    //获取模型

    Student *student = _dataArray[indexPath.row];

    cell.textLabel.text = student.name;

    cell.detailTextLabel.text = student.gender;

    return  cell;

    

}

 

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //push  和   传值

    

    DetailViewController *detailVC  =  [[DetailViewController  alloc] init];

    detailVC.student = _dataArray[indexPath.row];

    [self.navigationController pushViewController:detailVC animated:YES];

    

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

posted @ 2016-02-23 09:17  whwhll  阅读(123)  评论(0)    收藏  举报