UITableView的常用编辑操作
2015-06-01 11:47
453 查看
今天刚学了UItableView,在此总结了一些易忘的小操作。代理协议中的方法虽然很多,但是其实是有规律的,很多操作都是要先让某项操作可执行,然后再实现该方法。对应代码就是: 一个方法以BOOL为返回值,一个方法以void为返回值。下面的代码以hero为模型,该模型有三个属性,头像icon、名字name、介绍intro。具体代码如下
一、移动tableView中的行
1.要使每一行为可编辑状态
self.tableView.editing = YES;
2.使每一行行可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
3.控制器监听到移动后,交换模型数组中两个模型的数据.(不需要刷新)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[self.herosArray exchangeObjectAtIndex:destinationIndexPath.row withObjectAtIndex:sourceIndexPath.row];
}
二、tableView的复制、粘贴、剪贴操作
1.显示拷贝、粘贴选项板
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
2.实现某个方法(判断是不是拷贝、粘贴、剪切方法,是的话重新赋值模型数据,刷新)
// 在tableView的这一行执行某个方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
// 1.取出当前行的模型
Hero *hero = self.herosArray[indexPath.row];
// 1.1如果当前操作为拷贝操作
if (action == @selector(copy:)) {
// 1.2剪贴板记录下该模型的所有数据
[UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
}
// 如果为粘贴操作,给模型赋值新的数据,并刷新该行
if (action == @selector(paste:)) {
hero.name = [UIPasteboard generalPasteboard].strings[0];
hero.intro = [UIPasteboard generalPasteboard].strings[1];
hero.icon = [UIPasteboard generalPasteboard].strings[2];
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
// 如果当前为剪切操作
if (action == @selector(cut:)){
[UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
hero.name = nil;
hero.icon = nil;
hero.intro = nil;
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
三、在tableView中插入一行
1.修改编辑模式为插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert;
}
2.检测插入操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert) {
Hero *hero = [[Hero alloc] init];
hero.name = @"日番谷、冬狮郎";
hero.intro = @"十番队、队长";
hero.icon = @"1385536285032.png";
[self.herosArray insertObject:hero atIndex:indexPath.row];
// 插入一行,有动画,不需要手动刷新
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
四、在tableView中删除某一行
1.修改cell的编辑模式为删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewCellEditingStyleInsert;
return UITableViewCellEditingStyleDelete;
}
2.进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.herosArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
一、移动tableView中的行
1.要使每一行为可编辑状态
self.tableView.editing = YES;
2.使每一行行可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
3.控制器监听到移动后,交换模型数组中两个模型的数据.(不需要刷新)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[self.herosArray exchangeObjectAtIndex:destinationIndexPath.row withObjectAtIndex:sourceIndexPath.row];
}
二、tableView的复制、粘贴、剪贴操作
1.显示拷贝、粘贴选项板
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
2.实现某个方法(判断是不是拷贝、粘贴、剪切方法,是的话重新赋值模型数据,刷新)
// 在tableView的这一行执行某个方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
// 1.取出当前行的模型
Hero *hero = self.herosArray[indexPath.row];
// 1.1如果当前操作为拷贝操作
if (action == @selector(copy:)) {
// 1.2剪贴板记录下该模型的所有数据
[UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
}
// 如果为粘贴操作,给模型赋值新的数据,并刷新该行
if (action == @selector(paste:)) {
hero.name = [UIPasteboard generalPasteboard].strings[0];
hero.intro = [UIPasteboard generalPasteboard].strings[1];
hero.icon = [UIPasteboard generalPasteboard].strings[2];
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
// 如果当前为剪切操作
if (action == @selector(cut:)){
[UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
hero.name = nil;
hero.icon = nil;
hero.intro = nil;
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
三、在tableView中插入一行
1.修改编辑模式为插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert;
}
2.检测插入操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert) {
Hero *hero = [[Hero alloc] init];
hero.name = @"日番谷、冬狮郎";
hero.intro = @"十番队、队长";
hero.icon = @"1385536285032.png";
[self.herosArray insertObject:hero atIndex:indexPath.row];
// 插入一行,有动画,不需要手动刷新
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
四、在tableView中删除某一行
1.修改cell的编辑模式为删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewCellEditingStyleInsert;
return UITableViewCellEditingStyleDelete;
}
2.进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.herosArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- ios通过按钮点击异步加载图片
- ios中图像进行压缩方法汇总
- IOS检测指定路径的文件是否存在
- iOS、Mac OS X系统中编程实现汉字转拼音的方法(超级简单)