UITableViewDelete 删除
2015-11-18 11:02
393 查看
#import "ViewController.h"
#import "FWZViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *_arrayM;
UITableView *_tableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//编辑模式
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self getData];
[self addUITableView];
}
- (void)getData{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSArray *array = [userDefault objectForKey:@"Array"];
if (array == nil) {
_arrayM = [NSMutableArray array];
for (int i = 0; i < 10; i ++) {
NSString *str = [NSString stringWithFormat:@"我是第%d行",i];
[_arrayM addObject:str];
}
}else{
_arrayM = [NSMutableArray arrayWithArray:array];
}
}
- (void)addUITableView{
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *resuID = @"ID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuID];
}
cell.textLabel.text = _arrayM[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//1.进入下一页,并传值过去
FWZViewController *fwz = [[FWZViewController alloc]init];
fwz.cellName = _arrayM[indexPath.row];
[self.navigationController pushViewController:fwz animated:YES];
}
#pragma mark - 点击编辑按钮触发事件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//1.重写按钮编辑点击事件 首先要调用父类方法
[super setEditing:editing animated:YES];
//2.打开UItableview的编辑模式
[_tableView setEditing:editing animated:YES];
} //3.允许row编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//4.设置编辑样式
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCellEditingStyleNone,
// UITableViewCellEditingStyleDelete,
// UITableViewCellEditingStyleInsert
return UITableViewCellEditingStyleDelete;
}
//5.提交编辑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 从数据源删除数据
[_arrayM removeObjectAtIndex:indexPath.row];
[self userDefault];
//提交并刷新
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark - 编辑状态下可以移动cell
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);
NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);
// 取出目标数据
NSString *str = [_arrayM objectAtIndex:sourceIndexPath.row];
//删除目标数据
[_arrayM removeObjectAtIndex:sourceIndexPath.row];
[_arrayM insertObject:str atIndex:destinationIndexPath.row];
[self userDefault];
}
- (void)userDefault{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:_arrayM forKey:@"Array"];
NSLog(@"%@",_arrayM);
}
FWZViewController.h
#import <UIKit/UIKit.h>
@interface FWZViewController : UIViewController
@property (nonatomic,assign)NSString *cellName;
@end
FWZViewController.m
#import "FWZViewController.h"
#define SCREEN_W self.view.frame.size.width
@interface FWZViewController ()
{
UILabel *_label;
}
@end
@implementation FWZViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addUIlabel];
}
- (void)addUIlabel{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_W,100)];
_label.backgroundColor = [UIColor purpleColor];
_label.text = self.cellName;
_label.textColor = [UIColor blackColor];
[self.view addSubview:_label];
}
#import "FWZViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *_arrayM;
UITableView *_tableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//编辑模式
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self getData];
[self addUITableView];
}
- (void)getData{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSArray *array = [userDefault objectForKey:@"Array"];
if (array == nil) {
_arrayM = [NSMutableArray array];
for (int i = 0; i < 10; i ++) {
NSString *str = [NSString stringWithFormat:@"我是第%d行",i];
[_arrayM addObject:str];
}
}else{
_arrayM = [NSMutableArray arrayWithArray:array];
}
}
- (void)addUITableView{
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arrayM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *resuID = @"ID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuID];
}
cell.textLabel.text = _arrayM[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//1.进入下一页,并传值过去
FWZViewController *fwz = [[FWZViewController alloc]init];
fwz.cellName = _arrayM[indexPath.row];
[self.navigationController pushViewController:fwz animated:YES];
}
#pragma mark - 点击编辑按钮触发事件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//1.重写按钮编辑点击事件 首先要调用父类方法
[super setEditing:editing animated:YES];
//2.打开UItableview的编辑模式
[_tableView setEditing:editing animated:YES];
} //3.允许row编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//4.设置编辑样式
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCellEditingStyleNone,
// UITableViewCellEditingStyleDelete,
// UITableViewCellEditingStyleInsert
return UITableViewCellEditingStyleDelete;
}
//5.提交编辑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 从数据源删除数据
[_arrayM removeObjectAtIndex:indexPath.row];
[self userDefault];
//提交并刷新
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark - 编辑状态下可以移动cell
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);
NSLog(@"%ld %ld",(long)sourceIndexPath.row,(long)destinationIndexPath.row);
// 取出目标数据
NSString *str = [_arrayM objectAtIndex:sourceIndexPath.row];
//删除目标数据
[_arrayM removeObjectAtIndex:sourceIndexPath.row];
[_arrayM insertObject:str atIndex:destinationIndexPath.row];
[self userDefault];
}
- (void)userDefault{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:_arrayM forKey:@"Array"];
NSLog(@"%@",_arrayM);
}
FWZViewController.h
#import <UIKit/UIKit.h>
@interface FWZViewController : UIViewController
@property (nonatomic,assign)NSString *cellName;
@end
FWZViewController.m
#import "FWZViewController.h"
#define SCREEN_W self.view.frame.size.width
@interface FWZViewController ()
{
UILabel *_label;
}
@end
@implementation FWZViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addUIlabel];
}
- (void)addUIlabel{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, SCREEN_W,100)];
_label.backgroundColor = [UIColor purpleColor];
_label.text = self.cellName;
_label.textColor = [UIColor blackColor];
[self.view addSubview:_label];
}
相关文章推荐
- UITabView 添加
- Random_QuickSort
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout 还有drawRect方法
- 根据源码总结String,StringBuffer,StringBuider差异
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- UIButton篇
- 【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值
- UItableViewController添加UITouch事件(添加点击事件)
- MySQL 报错 Truncated incorrect INTEGER value: ''
- iOS9 Storyboard unwind segue反回传递事件时机详细步骤
- EasyUI 之datagrid 使用 【DataGrid属性解释】
- openstack通过qemu-guest-agent在物理机上操作虚拟机
- webpack + babel + vue 环境设置
- UIViewController懒加载模式_易懂版
- UIAlertController的AlertView形式 加输入框 用法
- ArcGIS 10.2 Calculate Value(Data Management) 工具的使用
- Arduino系列教程之 – PWM的秘密
- 关于UIViewController添加UINavigationBar的详细实现方法
- Xcode:CUICatalog: Invalid asset name supplied: (null)
- [转]百度UEditor编辑器(php)