iOS_13_tableView的编辑模式_红楼梦
来源:互联网 发布:计算机网络考研 知乎 编辑:程序博客网 时间:2024/06/06 12:56
最终效果图:
Girl.h
//// Girl.h// 12_tableView的增删改//// Created by beyond on 14-7-27.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <Foundation/Foundation.h>@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 <UIKit/UIKit.h>@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 ()<UITableViewDataSource,UITableViewDelegate>{ // 从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
1 0
- iOS_13_tableView的编辑模式_红楼梦
- iOS_12_tableViewCell的删除更新_红楼梦
- 永远的红楼梦
- 红楼梦命名的艺术
- 《红楼梦》的方位观念
- 《红楼梦》的香文化
- 《红楼梦》的香气空白
- 《红楼梦》的香气韵调
- 红楼梦
- 红楼梦
- 红楼梦
- 红楼梦
- 新版《红楼梦》的问题在哪儿?
- 红楼梦描写眼睛的词
- 你不知道的红楼梦
- 《红楼梦》的庭院叙事观点
- 《红楼梦》门的叙事视角
- 《红楼梦》香的祭祀文化
- Socket的全面解析和理解
- hdu1227 DP
- android 电池(一):锂电池基本原理篇
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第三步---主角开跑&同时带着刚体
- C#制作一个消息拦截器(intercept)4
- iOS_13_tableView的编辑模式_红楼梦
- phpwind安装,phpwind教程,phpwind安装教程,80端口被屏蔽动态IP无公网IP发布到外网
- HTML html5播放flash SWF播放器
- HDUJ 1042 N!
- HDU1158:Employment Planning(DP)
- 如何修改mysql默认空密码
- CCI 1.3 确定一个字符串重排列后,是否变成另一个字符串
- win7 64位+Oracle11g 64位下使用PLSQL Developer的解决办法
- MiediwWiki LDAP配置脚本