您的位置:首页 > 其它

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方法,否则右侧的删除按钮压根不会出现。

- (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
{

//这里什么都没有实现

//正常的话,应该在这里实现数据源的排序操作
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: