UITableViewCell的标记、移动、删除、插入

简介:
   这篇文章是建立在   

代码实现 UITableView与UITableViewCell基础上进行修改,用不上的代码我注释调,部分不明白可以看看上篇博客;实现的功能是对UITableViewCell的标记、移动、删除、插入;


1.标记:指的是选中某一行,在这一行后面有个符号,常见的是对勾形式
通过修改cell的accessoryType属性来实现,首先,在ViewDidLoad中[tableView setEditing:NO animated:YES];表示把单元格可编辑状态这只为NO
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];     if (cellView.accessoryType == UITableViewCellAccessoryNone) {         cellView.accessoryType=UITableViewCellAccessoryCheckmark;     }     else {         cellView.accessoryType = UITableViewCellAccessoryNone;         [tableView deselectRowAtIndexPath:indexPath animated:YES];     } }

 当我们选中单元格的时候,调用此函数,首先是indexPath检测选中了哪一行,if判断当前单元格是否被标记,也就是当前单元格风格,是否为 UITableViewCellAccessoryCheckmark风格,如果是,则换成UITableViewCellAccessoryNone(不被标记风格)风格,以下accessoryType四个风格属性
 UITableViewCellAccessoryCheckmark                 UITableViewCellAccessoryDetailDisclosureButton
        

UITableViewCellAccessoryDisclosureIndicator   UITableViewCellAccessoryNone
      

2.移动  
实现移动单元格就需要把单元格的编辑属性设置为YES,[tableView setEditing:YES animated:YES];
//返回YES,表示支持单元格的移动 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {     return YES; }
//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleInsert; }
三种风格的分别是

UITableViewCellEditingStyleDelete                                                UITableViewCellEditingStyleInsert

  


UITableViewCellEditingStyleNone


实现移动的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //    需要的移动行     NSInteger fromRow = [sourceIndexPath row]; //    获取移动某处的位置     NSInteger toRow = [destinationIndexPath row]; //    从数组中读取需要移动行的数据     id object = [self.listData objectAtIndex:fromRow]; //    在数组中移动需要移动的行的数据     [self.listData removeObjectAtIndex:fromRow]; //    把需要移动的单元格数据在数组中,移动到想要移动的数据前面     [self.listData insertObject:object atIndex:toRow];   }

单元格的移动是选中单元格行后面三条横线才可以实现移动的

  
3.删除
首先是判断( UITableViewCellEditingStyle )editingStyle,所以
//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleDelete; }

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {     if (editingStyle==UITableViewCellEditingStyleDelete) { //        获取选中删除行索引值         NSInteger row = [indexPath row]; //        通过获取的索引值删除数组中的值         [self.listData removeObjectAtIndex:row]; //        删除单元格的某一行时,在用动画效果实现删除过程         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];     } }

删除了张四 效果图:
     
4.添加
实现方法和删除方法相同,首先还是返回单元格编辑风格
//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleInsert; }

为了显示效果明显,在.h文件中声明一个变量i
#import <UIKit/UIKit.h>  @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> {  NSInteger i; } @property(strong,nonatomic) NSMutableArray *listData; @property(strong,nonatomic)UITableView *tableView; @property(strong,nonatomic)UITableViewCell *tableViewCell;  @end

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {     if (editingStyle==UITableViewCellEditingStyleDelete) { //        获取选中删除行索引值         NSInteger row = [indexPath row]; //        通过获取的索引值删除数组中的值         [self.listData removeObjectAtIndex:row]; //        删除单元格的某一行时,在用动画效果实现删除过程         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];     }     if(editingStyle==UITableViewCellEditingStyleInsert)     {                 i=i+1;         NSInteger row = [indexPath row];         NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];         NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i]; //        添加单元行的设置的标题         [self.listData insertObject:mes atIndex:row];         [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];     } }

运行效果图:
     

在删除和添加单元格的用到UITableViewRowAnimation动画效果,它还有其他几种效果,在此不做测试

UITableViewRowAnimationAutomatic      UITableViewRowAnimationTop 

UITableViewRowAnimationBottom          UITableViewRowAnimationLeft

UITableViewRowAnimationRight             UITableViewRowAnimationMiddle

UITableViewRowAnimationFade              UITableViewRowAnimationNone







     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208744 ,如需转载请自行联系原作者




相关文章
|
图形学 索引
「Unity」基于UnityWebRequest的HTTP文件断点续传
此处需要手动开启协程未考虑网络文件变动的问题,需要的话可以自行进行md5比对若需要分片下载,可以通过修改Range相关值实现 using System;using System.Collections;using System.
5861 0
Mac 快速查找快捷键command+f失效解决办法
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/81214817 最近 Chrome 经常的遇到 Command + F 快捷键失效的问题,真是日了狗了。
3787 0
sourcetree设置忽略文件
sourcetree设置忽略文件
560 0
|
Web App开发 iOS开发
三种获取苹果设备UID的方式
三种获取苹果设备UID的方式
3374 0
|
Android开发
MAC 安卓(Android) 编辑器无法识别真机
MAC 安卓(Android) 编辑器无法识别真机
290 0
|
缓存 iOS开发
iOS LaunchScreen.storyboard 启动页设置图片不显示
iOS LaunchScreen.storyboard 启动页设置图片不显示
574 0
UIcollectionViewCell(UItableviewcell)长按删除操作
功能简单,陷阱不少: 主要涉及两个功能: 1、长按手势:UILongPressGestureRecognizer 2、cell自带的删除操作:deleteItemsAtIndexPaths 首先为cell添加长按手势: UILongPressGestureRecognizer* longgs=[[UILongPressGestureRecognizer all
1624 0
|
小程序 前端开发 JavaScript
小程序上传多张图片到springboot后台,返回可供访问的图片链接
小程序上传多张图片到springboot后台,返回可供访问的图片链接
12040 0
|
SQL XML Java
Mybatis-Plus批量插入应该怎么用(下)
Mybatis-Plus批量插入应该怎么用
Mybatis-Plus批量插入应该怎么用(下)