知识点回顾-简单的TableView单组数据展示/多组数据展示
生活随笔
收集整理的這篇文章主要介紹了
知识点回顾-简单的TableView单组数据展示/多组数据展示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
總結
以上是生活随笔為你收集整理的知识点回顾-简单的TableView单组数据展示/多组数据展示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 43.放苹果(递归练习)
- 下一篇: 线段树-进阶