tableView各种模式(删除,移动,排序)
2013-09-07 19:34
239 查看
官网tableView的解释:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html
先来点理论,最后是一个demo:
当tableView进入编辑模式或者点击编辑的控制,tableView会向delegate发送一系列方法,但是只有代理实现了这些方法,这些方法允许代理重新显示cell的样式和行为。
(注意:当tableView不在编辑模式的时候也可以插入或者删除一系列行!!)
使用setEditing:animated:方法会让tableView进入编辑模式,编辑模式会根据代理实现的方法而由不同,(参见上一段的描述)
比方说实现了 - tableView: moveRowAtIndexPath: toIndexPath:那么编辑模式就还包括了可以移动cell的排序模式,否则没有.
实现了 - tableView:editingStyleForRowAtIndexPath:那么编辑模式根据不同cell,有删除和添加模式‘
滑动的时候要实现tableView:commitEditingStyle:forRowAtIndexPath方法,否则右侧的删除按钮压根不会出现。
可以尝试此方法,去让reload的时候不再有动画。。。
//Demo如下:
@interface
ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSInteger num;
}
@property (nonatomic,retain)
UITableView *tv;
@end
@implementation ViewController
- (void)dealloc
{
self.tv =
nil;
[super
dealloc];
}
- (void)viewDidLoad
{
[super
viewDidLoad];
num = 2;
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.frame =
CGRectMake(0,
0, 30,
30);
[btn addTarget:self
action:@selector(btnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
UIButton *insertBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
insertBtn.frame =
CGRectMake(50,
0, 30,
30);
[insertBtn addTarget:self
action:@selector(insertBtn:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:insertBtn];
self.tv = [[UITableView
alloc] initWithFrame:CGRectMake(0,
30,
320, 400)
style:UITableViewStylePlain];
self.tv.delegate =
self;
self.tv.dataSource =
self;
[self.view
addSubview:self.tv];
}
//按钮对应tableView的编辑模式
-(void)btnPressed:(id)sender
{
if (self.tv.editing ==
YES)
{
[self.tv
setEditing:NO
animated:YES];
}
else
{
[self.tv
setEditing:YES
animated:YES];
}
}
//插入cell的按钮
- (void)insertBtn:(id)sender
{
[self.tv
beginUpdates];
[self.tv
insertRowsAtIndexPaths:[NSArray
arrayWithObject:[NSIndexPath
indexPathForRow:0
inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
num ++;
[self.tv
endUpdates];
}
//进入编辑模式,确定编辑提交操作时,执行的代理方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle ==
UITableViewCellEditingStyleDelete)
{
[self.tv
beginUpdates];
[self.tv
deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
num = 1;
[self.tv
endUpdates];
}
if(editingStyle ==
UITableViewCellEditingStyleInsert)
{
[self.tv
beginUpdates];
[self.tv
insertRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tv
endUpdates];
}
}
//自定义cell的编辑模式,可以是删除也可以是增加 改变左侧的按钮的样式 删除是'-' 增加是'+'
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath {
if (indexPath.row ==
1) {
return
UITableViewCellEditingStyleInsert;
}
else {
return
UITableViewCellEditingStyleDelete;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSString *cellId =
@"fCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellId];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId] autorelease];
cell.contentView.backgroundColor = [UIColor
greenColor];
cell.accessoryType =
UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text =
@"fanfan";
cell.indentationLevel =
2;
cell.indentationWidth =
5; //缩进距离为2*5=10
cell.imageView.image = [UIImage
imageNamed:@"1"];
}
cell.detailTextLabel.text = [NSString
stringWithFormat:@"%d%d%d",indexPath.row,indexPath.row,indexPath.row];
return cell;
}
//实现这个方法之后,进入编辑模式,那么cell右边之后会出现可以编辑的按钮,长按然后拖动就可以了移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//这里什么都没有实现
//正常的话,应该在这里实现数据源的排序操作
}
先来点理论,最后是一个demo:
当tableView进入编辑模式或者点击编辑的控制,tableView会向delegate发送一系列方法,但是只有代理实现了这些方法,这些方法允许代理重新显示cell的样式和行为。
(注意:当tableView不在编辑模式的时候也可以插入或者删除一系列行!!)
使用setEditing:animated:方法会让tableView进入编辑模式,编辑模式会根据代理实现的方法而由不同,(参见上一段的描述)
比方说实现了 - tableView: moveRowAtIndexPath: toIndexPath:那么编辑模式就还包括了可以移动cell的排序模式,否则没有.
实现了 - tableView:editingStyleForRowAtIndexPath:那么编辑模式根据不同cell,有删除和添加模式‘
滑动的时候要实现tableView:commitEditingStyle:forRowAtIndexPath方法,否则右侧的删除按钮压根不会出现。
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
可以尝试此方法,去让reload的时候不再有动画。。。
//Demo如下:
@interface
ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSInteger num;
}
@property (nonatomic,retain)
UITableView *tv;
@end
@implementation ViewController
- (void)dealloc
{
self.tv =
nil;
[super
dealloc];
}
- (void)viewDidLoad
{
[super
viewDidLoad];
num = 2;
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.frame =
CGRectMake(0,
0, 30,
30);
[btn addTarget:self
action:@selector(btnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
UIButton *insertBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
insertBtn.frame =
CGRectMake(50,
0, 30,
30);
[insertBtn addTarget:self
action:@selector(insertBtn:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:insertBtn];
self.tv = [[UITableView
alloc] initWithFrame:CGRectMake(0,
30,
320, 400)
style:UITableViewStylePlain];
self.tv.delegate =
self;
self.tv.dataSource =
self;
[self.view
addSubview:self.tv];
}
//按钮对应tableView的编辑模式
-(void)btnPressed:(id)sender
{
if (self.tv.editing ==
YES)
{
[self.tv
setEditing:NO
animated:YES];
}
else
{
[self.tv
setEditing:YES
animated:YES];
}
}
//插入cell的按钮
- (void)insertBtn:(id)sender
{
[self.tv
beginUpdates];
[self.tv
insertRowsAtIndexPaths:[NSArray
arrayWithObject:[NSIndexPath
indexPathForRow:0
inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
num ++;
[self.tv
endUpdates];
}
//进入编辑模式,确定编辑提交操作时,执行的代理方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle ==
UITableViewCellEditingStyleDelete)
{
[self.tv
beginUpdates];
[self.tv
deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
num = 1;
[self.tv
endUpdates];
}
if(editingStyle ==
UITableViewCellEditingStyleInsert)
{
[self.tv
beginUpdates];
[self.tv
insertRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tv
endUpdates];
}
}
//自定义cell的编辑模式,可以是删除也可以是增加 改变左侧的按钮的样式 删除是'-' 增加是'+'
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath {
if (indexPath.row ==
1) {
return
UITableViewCellEditingStyleInsert;
}
else {
return
UITableViewCellEditingStyleDelete;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSString *cellId =
@"fCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellId];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId] autorelease];
cell.contentView.backgroundColor = [UIColor
greenColor];
cell.accessoryType =
UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text =
@"fanfan";
cell.indentationLevel =
2;
cell.indentationWidth =
5; //缩进距离为2*5=10
cell.imageView.image = [UIImage
imageNamed:@"1"];
}
cell.detailTextLabel.text = [NSString
stringWithFormat:@"%d%d%d",indexPath.row,indexPath.row,indexPath.row];
return cell;
}
//实现这个方法之后,进入编辑模式,那么cell右边之后会出现可以编辑的按钮,长按然后拖动就可以了移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//这里什么都没有实现
//正常的话,应该在这里实现数据源的排序操作
}
相关文章推荐
- 源码推荐(9.17):长按按钮可以移动自动排序,tableView多选 可存储文件
- CYC- tableView的删除编辑移动
- [11]UITableView编辑 删除 添加 移动 单例 及其去掉TableView没内容的Cell
- tableView中cell的删除、插入、移动、复制粘贴问题详解代码分析
- tableView编辑模式下删除多个cell
- 编辑表格@tableView实现增加、删除、移动的操作
- 与TableView插入、删除、移动、多选,刷新控件
- ITableView的删除,移动及修改删除按钮上的文字,来自,dick_china
- tableView编辑模式下删除多个cell
- IOS tableView 滑动删除与排序功能
- 关于tableView点击cell中按钮进入排序模式(cell行高统一可长按排序)
- UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件
- Snail—UI学习之表视图TableView单行添加、删除和移动
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除
- iOS tableView删除,添加,排序方法实现
- editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)
- 仿tableview接口实现zaker首页效果(画n*m布局、拖动排序、删除)
- iOS tableView 如何禁止滑动手势删除,只允许在编辑模式下进行删除Cell
- tableView编辑:插入、删除和移动
- ios tableView那些事 (八) tableview的插入移动