iOS_13_tableView的編輯模式紅樓夢
最終:
Girl.h
<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">//// Girl.h// 12_tableView的增刪改//// Created by beyond on 14-7-27.// 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;// 類方法,字典 轉 對象 類似javaBean一次性填充+ (Girl *)girlWithDict:(NSDictionary *)dict;// 對象方法,設定對象的屬性後,返回對象- (Girl *)initWithDict:(NSDictionary *)dict;@end
Girl.m
//// Girl.m// 12_tableView的增刪改//// Created by beyond on 14-7-27.// 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;}// 類方法,字典 轉 對象 類似javaBean一次性填充+ (Girl *)girlWithDict:(NSDictionary *)dict{ // 只是調用對象的initWithDict方法,之所以用self是為了對子類進行相容 return [[self alloc]initWithDict:dict];}// 對象方法,設定對象的屬性後,返回對象- (Girl *)initWithDict:(NSDictionary *)dict{ // 先調用父類NSObject的init方法 if (self = [super init]) { // 設定對象自己的屬性 self.name = dict[@"name"] ; self.headImgName = dict[@"headImg"] ; self.verdict = dict[@"verdict"]; } // 返回填充好的對象 return self;}@end
BeyondViewController.h
//// BeyondViewController.h// 13_tableView的編輯模式//// Created by beyond on 14-7-28.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import @interface BeyondViewController : UIViewController// 點擊 刪除,進入編輯模式之刪除 模式- (IBAction)trashBtnClick:(UIBarButtonItem *)sender;@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (weak, nonatomic) IBOutlet UIBarButtonItem *trashBtn;@end
BeyondViewController.m
//// BeyondViewController.m// 13_tableView的編輯模式//// Created by beyond on 14-7-28.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"#import "Girl.h"@interface BeyondViewController (){ // 從plist檔案中載入的所有girls,返回字典數組 NSArray *_arrayWithDict; // 所有的對象數組 NSMutableArray *_girls;}@end@implementation BeyondViewController- (void)viewDidLoad{ [super viewDidLoad]; NSLog(@"view did load"); // 所有的對象數組 _girls = [NSMutableArray array]; // 調用自訂方法,載入plist檔案 [self loadPlist]; }// 自訂方法,載入plist檔案- (void)loadPlist{ // sg_bundle模板代碼,1,獲得.app主要的包;2,返回主要的包中某個檔案的fullPath全路徑 NSBundle *mainBundle = [NSBundle mainBundle]; NSString *fullPath = [mainBundle pathForResource:@"girls.plist" ofType:nil]; // 從plist檔案中根據全路徑,返回字典數組 _arrayWithDict = [NSArray arrayWithContentsOfFile:fullPath]; // 再調用自訂方法,將字典數組,轉換成對象數組 [self dictArrayToModelArray]; }// 自訂方法,將字典數組,轉換成對象數組- (void)dictArrayToModelArray{ // 字典數組 _arrayWithDict // 方式2:類方法返回對象,參數只要一個字典數組即可 for (NSDictionary *dict in _arrayWithDict) { // 參數只要字典,這樣一來,控制器就不用知道太多東西了 // Girl *girl = [[Girl alloc]initWithDict:dict]; Girl *girl = [Girl girlWithDict:dict]; [_girls addObject:girl]; }}// 點擊刪除按鈕,進入編輯模式- (IBAction)trashBtnClick:(UIBarButtonItem *)sender { // 如果正在編輯,則取消編輯,否則 開始編輯 if ([_tableView isEditing]) { //_tableView.editing = NO; [_tableView setEditing:NO animated:YES]; } else { //_tableView.editing = YES; [_tableView setEditing:YES animated:YES]; }}#pragma mark - 代理方法// 編輯模式下,點擊一行的內建的刪除按鈕時調用代理的該commitEditing方法,並且,只要實現了該方法之後,只要手指在螢幕上向左滑動,就會自動進入編輯模式,彈出cell中的刪除 按鈕- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"commitEditing---row--%d",indexPath.row); // 擷取編輯模式 UITableViewCellEditingStyle style = editingStyle; // 擷取要刪除的行 int row = indexPath.row; // 如果是刪除模式,才往下進行 if (style != UITableViewCellEditingStyleDelete) { return ; } // 先修改資料 模型,再更新(如果對象數組中刪除了一些對象,則只能調用tableView的deleteRowsAtIndexPaths方法,否則報錯) [_girls removeObjectAtIndex:row]; [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // 調用自訂方法,負責按鈕標題等狀態檢查 [self statusCheck]; }// 自訂方法,負責按鈕標題等狀態檢查- (void)statusCheck{ // 如果沒有東西可以刪除,則刪除 按鈕禁用 if (_girls.count == 0) { _trashBtn.enabled = NO; }}// 代理方法,排序,moveRow- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 只要實現此方法,就可以自動拖拽cell進行排列了 //修改資料,等到再次顯示的時候,調用cellForRow就會依然按最新的順序正確顯示了 Girl *girl = [_girls objectAtIndex:sourceIndexPath.row]; [_girls removeObject:girl]; [_girls insertObject:girl atIndex:destinationIndexPath.row]; }// 代理方法,點擊行時,取消點擊時的高亮的背景顏色- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [_tableView deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark - 資料來源方法// 資料來源方法,每一組,有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 返回數組中對應的對象個數 return _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:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } // 設定cell中獨一無二的內容 Girl *girl = [_girls objectAtIndex:indexPath.row]; cell.textLabel.text = girl.name; cell.imageView.image = [UIImage imageNamed:girl.headImgName]; cell.detailTextLabel.text = girl.verdict; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 返回cell return cell;}// 代理方法,每一行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 83;}@end
顯示介面預設的英文改成中文
girls.plist
如果資料來源(即對象數組)和tableView的row 刪除不統一,就會報錯
cell中的其實還有一個中介軟體contentView