1 拖入TableView到UIView中,连线DataSource
2
3 1.实现数据源方法
4 - (NSInteger)tableView:(UITableView *
)tableView numberOfRowsInSection:(NSInteger)section
5 {
6 return ;
7 }
8
9 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath
10 {
11 return ;
12 }
13
14 2.根据指定的的cell样式创建cell
15 UITableViewCellStyleDefault
16 UITableViewCellStyleValue1
17 UITableViewCellStyleValue2
18 UITableViewCellStyleSubtitle
19
20 3.解析字典—>字典转模型—>
取出indexPath位置对应的XMGWine模型
21
22 4.设置数据(用创建出来的cell对象直接点出里面子控件设置)
23
24 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath
25 {
26 // 创建一个UITableViewCellStyleSubtitle样式的cell
27 UITableViewCell *cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
28
29 // 取出indexPath位置对应的XMGWine模型
30 XMGWine *wine =
self.wineArray[indexPath.row];
31
32 // 设置数据
33 cell.imageView.image =
[UIImage imageNamed:wine.image];
34 cell.textLabel.text =
wine.name;
35 cell.detailTextLabel.text = [NSString stringWithFormat:
@"¥%@", wine.money];
36 cell.detailTextLabel.textColor =
[UIColor orangeColor];
37 //cell最右边的显示样式('>')
38 cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
39
40 return cell;
41 }
42
43 多组数据展示
44 跟单组数据展示一样,但是数组里面又有数组,需要逐层转模型
45 /********************************************************
46 1> plist解析:
47 以后遇到像这种复杂的plist,一层一层的往下解析.
48 最终的目的就是将所有的字典转成模型.
49 如果plist中字典在一个数组中,将来转出来的模型也放在一个数组中.
50 也就是将字典数组转成模型数组.
51
52 2> plist的好处:方便管理数据
53 *********************************************************/
54
55 设置数据需要先拿出组模型,再拿出行模型
56
57 #pragma mark - <UITableViewDataSource>
58 - (NSInteger)numberOfSectionsInTableView:(UITableView *
)tableView
59 {
60 return self.carGroups.count;
61 }
62
63 - (NSInteger)tableView:(UITableView *
)tableView numberOfRowsInSection:(NSInteger)section
64 {
65 XMGCarGroup *carGroup =
self.carGroups[section];
66 return carGroup.cars.count;
67 }
68
69 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath
70 {
71 UITableViewCell *cell =
[[UITableViewCell alloc] init];
72
73 // 设置cell右边的指示样式
74 cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
75
76 // 取出indexPath位置对应的XMGCar模型
77 XMGCarGroup *carGroup =
self.carGroups[indexPath.section];
78 XMGCar *car =
carGroup.cars[indexPath.row];
79
80 cell.textLabel.text =
car.name;
81 cell.imageView.image =
[UIImage imageNamed:car.icon];
82 return cell;
83 }
84
85 /**
86 * 告诉tableView第section组的头部标题文字
87 */
88 - (NSString *)tableView:(UITableView *
)tableView titleForHeaderInSection:(NSInteger)section
89 {
90 XMGCarGroup *carGroup =
self.carGroups[section];
91 return carGroup.header;
92 }
93
94 /**
95 * 告诉tableView第section组的尾部标题文字
96 */
97 - (NSString *)tableView:(UITableView *
)tableView titleForFooterInSection:(NSInteger)section
98 {
99 XMGCarGroup *carGroup =
self.carGroups[section];
100 return carGroup.footer;
101 }
102
103 @implementation XMGCarGroup
104 + (instancetype)carGroupWithDict:(NSDictionary *
)dict
105 {
106 XMGCarGroup *group =
[[self alloc] init];
107 group.header = dict[
@"header"];
108 group.footer = dict[
@"footer"];
109
110 // 将字典数组(装着车的字典) -> 模型数据(装着车的模型)
111 NSMutableArray *cars =
[NSMutableArray array];
112 for (NSDictionary *carDict
in dict[
@"cars"]) {
113 [cars addObject:[XMGCar carWithDict:carDict]];
114 }
115 group.cars =
cars;
116
117 return group;
118 }
make by-LJW
转载于:https://www.cnblogs.com/ljwiOS/p/5243988.html