iOS_10_tableView的簡單使用_紅樓十二釵

來源:互聯網
上載者:User

標籤: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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.