標籤:ios
最終:
方式1,用字典數組
BeyondViewController.h
//// BeyondViewController.h// 10_tableView//// Created by beyond on 14-7-25.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController@end
BeyondViewController.m
//// BeyondViewController.m// 10_tableView//// Created by beyond on 14-7-25.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"#define kHeader @"header"#define kFooter @"footer"#define kGirlsArr @"girls"@interface BeyondViewController ()<UITableViewDataSource>{ // 假資料 NSArray *_array;}@end@implementation BeyondViewController- (void)viewDidLoad{ [super viewDidLoad]; // 假資料 方式1 用字典 _array = @[ @{kHeader: @"十二釵正冊", kGirlsArr:@[@"林黛玉",@"薛寶釵",@"賈元春",@"賈探春",@"史湘雲",@"妙玉",@"賈迎春",@"賈惜春",@"王熙鳳",@"賈巧姐",@"李紈",@"秦可卿"], kFooter:@"紅樓夢" }, @{kHeader: @"十二釵副冊", kGirlsArr:@[@"香菱",@"薛寶琴",@"尤二姐",@"尤三姐",@"邢岫煙",@"李紋",@"李綺",@"夏金桂",@"秋桐",@"小紅",@"齡官",@"嬌杏"], kFooter:@"紅樓夢" }, @{kHeader: @"十二釵又副冊", kGirlsArr:@[@"晴雯",@"襲人",@"平兒",@"鴛鴦",@"紫鵑",@"鶯兒",@"玉釧",@"金釧",@"彩雲",@"司棋",@"芳官",@"麝月"], kFooter:@"紅樓夢" } ]; // 假資料 方式2 用類封裝 // 樣式只有兩種 Grouped PlainUITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; // 資料來源 tableView.dataSource = self; // 添加到self.view [self.view addSubview:tableView];}// 資料來源方法,特例,重要~ 一共有多少個分組- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _array.count;}// 資料來源方法,每一組,有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 返回數組中對應的字典的長度 return [[_array[section] objectForKey:kGirlsArr] count];}// 資料來源方法,每一組的每一行應該顯示怎麼的介面(含封裝的資料),重點!!!必須實現否則,Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellID = @"Beyond"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { // 如果池中沒取到,則重建一個cell /* cell的4種樣式: 1,default 左圖右文字 2,subtitle 左圖 上文字大 下文字小 3,value 1 左圖 左文字大 右文字小 3,value 2 噁心 左文字小 右文字大 */ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } // 設定cell中獨一無二的內容 cell.textLabel.text = [_array[indexPath.section] objectForKey:kGirlsArr][indexPath.row]; // 返回cell return cell;}// 資料來源方法,組的開頭顯示什麼標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [_array[section] objectForKey:kHeader];}// 資料來源方法,,組的最後顯示什麼標題//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section//{// return [_array[section] objectForKey:kFooter];//}@end
方式2,用類(model)代替數組中的字典
TwelveBeauties.h
//// TwelveBeauties.h// 10_tableView//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <Foundation/Foundation.h>// 對應 viewController.m中的成員數組中的一個成員 --> 字典@interface TwelveBeauties : NSObject// UI控制項連線時用weak,字串用copy,其他對象用strong@property (nonatomic,copy) NSString *header;@property (nonatomic,copy) NSString *footer;@property (nonatomic,strong) NSArray *girls;// 提供一個類方法,一個以類名開頭的構造方法(返回id亦可)+ (TwelveBeauties *)twelveBeautiesWithHeader:(NSString *)header footer:(NSString *)footer girls:(NSArray *)girls;@end
TwelveBeauties.m
//// TwelveBeauties.m// 10_tableView//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "TwelveBeauties.h"@implementation TwelveBeauties// 提供一個類方法,一個以類名開頭的構造方法(返回id亦可)+ (TwelveBeauties *)twelveBeautiesWithHeader:(NSString *)header footer:(NSString *)footer girls:(NSArray *)girls{ TwelveBeauties *twelveBeauties = [[TwelveBeauties alloc]init]; twelveBeauties.header = header; twelveBeauties.footer = footer; twelveBeauties.girls = girls; return twelveBeauties;}@end
BeyondViewController.m
//// BeyondViewController.m// 10_tableView//// Created by beyond on 14-7-25.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"#import "TwelveBeauties.h"@interface BeyondViewController ()<UITableViewDataSource>{ // 假資料 NSArray *_array;}@end@implementation BeyondViewController- (void)viewDidLoad{ [super viewDidLoad];// 假資料 方式1 用字典/* _array = @[ @{kHeader: @"十二釵正冊", kGirlsArr:@[@"林黛玉",@"薛寶釵",@"賈元春",@"賈探春",@"史湘雲",@"妙玉",@"賈迎春",@"賈惜春",@"王熙鳳",@"賈巧姐",@"李紈",@"秦可卿"], kFooter:@"紅樓夢" }, @{kHeader: @"十二釵副冊", kGirlsArr:@[@"香菱",@"薛寶琴",@"尤二姐",@"尤三姐",@"邢岫煙",@"李紋",@"李綺",@"夏金桂",@"秋桐",@"小紅",@"齡官",@"嬌杏"], kFooter:@"紅樓夢" }, @{kHeader: @"十二釵又副冊", kGirlsArr:@[@"晴雯",@"襲人",@"平兒",@"鴛鴦",@"紫鵑",@"鶯兒",@"玉釧",@"金釧",@"彩雲",@"司棋",@"芳官",@"麝月"], kFooter:@"紅樓夢" } ];*/ // 假資料 方式2 用類封裝後 _array = @[ [TwelveBeauties twelveBeautiesWithHeader:@"十二釵正冊" footer:@"紅樓夢" girls:@[">@"林黛玉",@"薛寶釵",@"賈元春",@"賈探春",@"史湘雲",@"妙玉",@"賈迎春",@"賈惜春",@"王熙鳳",@"賈巧姐",@"李紈",@"秦可卿"]], [TwelveBeauties twelveBeautiesWithHeader:@"十二釵副冊" footer:@"紅樓夢" girls:@[@"香菱",@"薛寶琴",@"尤二姐",@"尤三姐",@"邢岫煙",@"李紋",@"李綺",@"夏金桂",@"秋桐",@"小紅",@"齡官",@"嬌杏"]], [TwelveBeauties twelveBeautiesWithHeader:@"十二釵又副冊" footer:@"紅樓夢" girls:@[@"晴雯",@"襲人",@"平兒",@"鴛鴦",@"紫鵑",@"鶯兒",@"玉釧",@"金釧",@"彩雲",@"司棋",@"芳官",@"麝月"]] ]; // 樣式只有兩種 Grouped PlainUITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; // 資料來源 tableView.dataSource = self; // 添加到self.view [self.view addSubview:tableView];}// 資料來源方法,特例,重要~ 一共有多少個分組- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _array.count;}// 資料來源方法,每一組,有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 返回數組中對應的字典的長度 // return [[_array[section] objectForKey:kGirlsArr] count]; // 用資料模型封裝後 TwelveBeauties *twelveBeauties = _array[section]; return twelveBeauties.girls.count;}// 資料來源方法,每一組的每一行應該顯示怎麼的介面(含封裝的資料),重點!!!必須實現否則,Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellID = @"Beyond"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { // 如果池中沒取到,則重建一個cell /* cell的4種樣式: 1,default 左圖右文字 2,subtitle 左圖 上文字大 下文字小 3,value 1 左圖 左文字大 右文字小 3,value 2 噁心 左文字小 右文字大 */ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } // 設定cell中獨一無二的內容,字典封裝假資料 //cell.textLabel.text = [_array[indexPath.section] objectForKey:kGirlsArr][indexPath.row]; // 用資料模型封裝後 TwelveBeauties *twelveBeauties = _array[indexPath.section]; cell.textLabel.text = [twelveBeauties.girls objectAtIndex:indexPath.row] ; // 返回cell return cell;}// 資料來源方法,組的開頭顯示什麼標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ // 字典封裝假資料 // return [_array[section] objectForKey:kHeader]; // 用資料模型封裝後 TwelveBeauties *twelveBeauties = _array[section]; return twelveBeauties.header;}// 資料來源方法,組的索引的標題(通訊錄最右邊的豎條)-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ // 用KVC \ KVO 可以一句代碼實現 return @[@"正冊",@"副冊",@"又副冊"] ;// NSMutableArray *array = [NSMutableArray array];// for (TwelveBeauties *tb in _array) {// [array addObject:tb.header];// }// NSLog(@"%@",array);// return array; }@end