iOS_12_tableViewCell的簡單使用
最終:
Girl.h
//// Girl.h// 11_tableView的使用_紅樓夢//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import @interface Girl : NSObject// UI控制項用weak,字串用copy,其他對象用strong// 頭像圖片名@property(nonatomic,copy)NSString *headImgName;// 姓名@property(nonatomic,copy)NSString *name;// 判詞@property(nonatomic,copy)NSString *verdict;// 提供一個類方法,即建構函式,返回封裝好資料的對象(返回id亦可)+ (Girl *)girlNamed:(NSString *)name headImgName:(NSString*)headImgName verdict:(NSString *)verdict;@end
Girl.m
//// Girl.m// 11_tableView的使用_紅樓夢//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "Girl.h"@implementation Girl// 提供一個類方法,即建構函式,返回封裝好資料的對象(返回id亦可)+(Girl *)girlNamed:(NSString *)name headImgName:(NSString *)headImgName verdict:(NSString *)verdict{ Girl *girl = [[Girl alloc]init]; girl.name = name; girl.headImgName = headImgName; girl.verdict = verdict; return girl;}@end
BeyondViewController.h
//// BeyondViewController.h// 11_tableView的使用_紅樓夢//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import @interface BeyondViewController : UIViewController// 重新重新整理表格時要用到@property (weak, nonatomic) IBOutlet UITableView *tableView;@end
BeyondViewController.m
//// BeyondViewController.m// 11_tableView的使用_紅樓夢//// Created by beyond on 14-7-26.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"#import "Girl.h"@interface BeyondViewController (){ // 存放假資料 NSMutableArray *_array;}@end@implementation BeyondViewController- (void)viewDidLoad{ [super viewDidLoad]; _array = [NSMutableArray array];// 假資料 [_array addObject:[Girl girlNamed:@"林黛玉" headImgName:@"0.png" verdict:@"可歎停機德,堪憐詠絮才。玉帶林中掛,金簪雪裡埋。"]]; [_array addObject:[Girl girlNamed:@"薛寶釵" headImgName:@"1.png" verdict:@"可歎停機德,堪憐詠絮才。玉帶林中掛,金簪雪裡埋。"]]; [_array addObject:[Girl girlNamed:@"妙玉" headImgName:@"2.png" verdict:@"欲潔何曾潔,雲空未必空。可憐金玉質,終陷淖泥中。"]]; [_array addObject:[Girl girlNamed:@"史湘雲" headImgName:@"3.png" verdict:@"富貴又何為?繈褓之間父母違。展眼吊斜輝,湘江水逝楚雲飛。"]]; [_array addObject:[Girl girlNamed:@"探春" headImgName:@"4.png" verdict:@"才自清明志自高,生於末世運偏消。清明涕泣江邊望,千裡東風一夢遙。"]]; [_array addObject:[Girl girlNamed:@"惜春" headImgName:@"5.png" verdict:@"堪破三春景不常,緇衣頓改昔年妝。可憐秀戶侯門女,獨臥青燈古佛旁。"]]; [_array addObject:[Girl girlNamed:@"王熙鳳" headImgName:@"6.png" verdict:@"凡鳥偏從末世來,都知愛慕此生才。一從二令三人木,哭向金陵事可哀。 "]]; }#pragma mark - tableView的資料來源方法// 資料來源方法,特例,重要~ 一共有多少個分組 (預設就是返回1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // 單組資料顯示,無需分組,故返回 1,(預設就是返回1) return 1;}// 資料來源方法,每一組,有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 7;}// 資料來源方法,每一組的每一行應該顯示怎麼的介面(含封裝的資料),重點!!!// 每當有一行cell 進入視野範圍就會調用// 必須實現否則,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:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } // 設定cell中獨一無二的內容 Girl *girl = _array[indexPath.row]; cell.imageView.image = [UIImage imageNamed:girl.headImgName]; cell.textLabel.text = girl.name; cell.detailTextLabel.text = girl.verdict; // 設定單元的右邊附屬 // cell.accessoryType = UITableViewCellAccessoryDetailButton; // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 返回cell return cell;}#pragma mark - tableView的代理方法// 代理方法,每一行多高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 93;}// 代理方法,將要點擊某一行的時候調用 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"will select----%d",indexPath.row); return indexPath;}// 代理方法,當點擊一行時調用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"did select----%d",indexPath.row); [tableView deselectRowAtIndexPath:indexPath animated:YES]; Girl *girl = _array[indexPath.row]; // 彈出姓名,以供使用者更改 // 設定代理的目的是響應alert的按鈕點擊事件 //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:girl.name message:girl.verdict delgate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"other", nil]; UIAlertView *alert = [[UIAlertView alloc]init]; [alert initWithTitle:girl.name message:girl.verdict delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; // alertViewStyle 樣式----密碼 // alert.alertViewStyle = UIAlertViewStyleSecureTextInput; // alertViewStyle 樣式----一般的文本輸入框 alert.alertViewStyle = UIAlertViewStylePlainTextInput; // alertViewStyle 樣式----使用者名稱及密碼登入框 // alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; // alertViewStyle 樣式----標籤顯示 // alert.alertViewStyle = UIAlertViewStyleDefault; // 使用者名稱密碼的情況下有兩個文字框 UITextField *textField = [alert textFieldAtIndex:0]; textField.text = girl.name; // 關鍵代碼,通過tag將點擊的行號帶給alertView的代理方法,還可以通過利用代理即控制器的成員進行 行號 的傳遞~ textField.tag = indexPath.row; // 顯示alertView [alert show]; /* 預設情況下,上面的alert是局部變數,在本方法調完的時候,會被釋放 但是,[alert show]方法,會有一種機制(比如UIWindow會持有它的引用,使之不被銷毀) */}// 代理方法,當取消點擊一行時調用- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"did deselect row----%d",indexPath.row);}#pragma mark - UIAlertViewDelegate的代理方法- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // 查看點擊了alertView裡面的哪一個按鈕,取消按鈕是 0 NSLog(@"alertView裡面的按鈕index---%d",buttonIndex); if (buttonIndex == 0) { // 0代表取消按鈕 return; }else if (buttonIndex == 1){ // 1代表確定按鈕,更新資料來源,重新載入資料 UITextField *textField = [alertView textFieldAtIndex:0]; NSString *newName = [textField text]; // robust判斷 if ([newName isEqualToString:@""]) { return; } // 先更新資料來源 int row = textField.tag; Girl *girl = _array[row]; girl.name = newName; // 再,全部重新載入資料 // [_tableView reloadData]; // 最好是,局部重新整理資料,通過row產生一個一個indexPath組成數組 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; NSArray *indexPaths = @[indexPath]; [_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; }}@end
tableViewCellAccessory
<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+dGFibGVWaWV3Q2VsbFN0eWxlPGJyPgo8L3A+CjxwPjxicj4KPC9wPgo8cD48aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140728/2014072809125187.png" alt="\">
tableView資料來源