码迷,mamicode.com
首页 > 移动开发 > 详细

[iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示

时间:2014-12-01 06:25:28      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

A.实现思路
1.拖入UITableView
2.拖曳、连线UITableView控件
3.Controller遵守UITalbeViewDataSource协议
4.设置UITableView的dataSource
5.加载数据到Model
6.从Model解析数据,显示到View上
 
bubuko.com,布布扣
 
 
B.实现细节
1.UITableView style
(1)Grouped,成组出现,标题和尾部会被分隔开,如上图
bubuko.com,布布扣
 
(2)Plain
bubuko.com,布布扣
 
bubuko.com,布布扣
 
 
C.主要代码
Car.h
 1 //
 2 //  Car.h
 3 //  01-CarBrand
 4 //
 5 //  Created by hellovoidworld on 14/11/30.
 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Car : NSObject
12 
13 @property(nonatomic, strong) NSArray *cars;
14 @property(nonatomic, copy) NSString *title;
15 @property(nonatomic, copy) NSString *desc;
16 
17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
18 + (instancetype) carWithDictionary:(NSDictionary *) dictionary;
19 + (instancetype) car;
20 
21 @end
 
Car.m
 1 //
 2 //  Car.m
 3 //  01-CarBrand
 4 //
 5 //  Created by hellovoidworld on 14/11/30.
 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "Car.h"
10 
11 @implementation Car
12 
13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
14     if (self == [super init]) {
15         self.cars = dictionary[@"cars"];
16         self.title = dictionary[@"title"];
17         self.desc = dictionary[@"desc"];
18     }
19    
20     return self;
21 }
22 
23 + (instancetype) carWithDictionary:(NSDictionary *) dictionary {
24     return [[self alloc] initWithDictionary:dictionary];
25 }
26 
27 + (instancetype) car {
28     return [self carWithDictionary:nil];
29 }
30 
31 @end
 
ViewController.m
 1 //
 2 //  ViewController.m
 3 //  01-CarBrand
 4 //
 5 //  Created by hellovoidworld on 14/11/30.
 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "Car.h"
11 
12 @interface ViewController () <UITableViewDataSource>
13 
14 @property (weak, nonatomic) IBOutlet UITableView *tableView;
15 
16 @property(nonatomic, strong) NSArray *allBrandOfCars;
17 
18 @end
19 
20 @implementation ViewController
21 
22 - (void)viewDidLoad {
23     [super viewDidLoad];
24     // Do any additional setup after loading the view, typically from a nib.
25    
26     self.tableView.dataSource = self;
27 }
28 
29 - (void)didReceiveMemoryWarning {
30     [super didReceiveMemoryWarning];
31     // Dispose of any resources that can be recreated.
32 }
33 
34 
35 #pragma mark - dataSource方法
36 
37 /** Sections 数,组数 */
38 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
39     return self.allBrandOfCars.count; // 所有车的派系的数量
40 }
41 
42 /** 组内的行数 */
43 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
44     Car *car = self.allBrandOfCars[section];
45     return car.cars.count; // 每个派系的车的牌子的数量
46 }
47 
48 /** 组的标题
49 这里是车的派系
50 */
51 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
52     Car *car = self.allBrandOfCars[section];
53     return  car.title;
54 }
55 
56 /** 组的尾部 */
57 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
58     Car *car = self.allBrandOfCars[section];
59     return  car.desc;
60 }
61 
62 /** 行的内容 */
63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
64     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
65    
66     Car *car = self.allBrandOfCars[indexPath.section]; // 车的派系
67     NSString *carName = car.cars[indexPath.row]; // 具体车的牌子
68    
69     cell.textLabel.text = carName;
70    
71     return  cell;
72 }
73 
74 
75 /** 延迟加载plist文件中的数据 */
76 - (NSArray *) allBrandOfCars {
77     if (nil == _allBrandOfCars) {
78         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_simple" ofType:@"plist"]];
79        
80         NSMutableArray *mdictArray = [NSMutableArray array];
81         for (NSDictionary *dict in dictArray) {
82             Car *car = [Car carWithDictionary:dict];
83             [mdictArray addObject:car];
84         }
85        
86         _allBrandOfCars = mdictArray;
87     }
88 
89     return _allBrandOfCars;
90 }
91 
92 
93 @end
 
 

[iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/hellovoidworld/p/4134086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!