UITableViewCell单元格的删除、插入、移动

UITableViewDelegate的方法

设置编辑模式中得cell的编辑样式(删除或插入)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableViewDataSource的方法

     

     设置该单元格能否被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

     设置该单元格能否被移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

对单元格进行编辑时会调用此方法

删除单元格:1)先删除数据源 2)接着删除单元格 3)刷新单元格

插入单元格:1)先插入数据源 2)接着插入单元格 3)刷新单元格

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

//判断编辑样式(删除或插入)

if (editingStyle==UITableViewCellEditingStyleDelete)

{

//必须要先删除数据源

NSMutableArray *arr=[self.dataSourceArray objectAtIndex:indexPath.section];

[arr removeObjectAtIndex:indexPath.row];

//接着删除单元格(参数是数组,可能删除多行)
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

//删除完后要刷新tableView
          [tableView reloadData];
     }

else if (editingStyle==UITableViewCellEditingStyleInsert)  //插入模式
     {

//下面根据实际情况
          CSFriends *friend=[[CSFriends alloc]init];
          friend.imageName=[NSString stringWithFormat:@"%d.jpg",2];
          [email protected]"超级布罗利";
          [email protected]"超级赛亚人";
          //必须要先插入数据源
          NSMutableArray *arr=[self.dataSourceArray objectAtIndex:indexPath.section];

[arr insertObject:friend atIndex:indexPath.row];

//接着插入单元格(参数是数组,可能插入多行)
          [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

//插入后要刷新tableView
          [tableView reloadData];
    }

}

     对单元格进行移动时会调用此方法

 

移动单元格:1)先将要移动的数据从数据源的原位置中删除 2)接着再讲数据插入数据源的新位置 3)刷新单元格

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

//先找到要移动的数据(sourceIndexPath是移动前的位置)

NSMutableArray *array=self. dataSourceArray[sourceIndexPath.section];

CSFriends *friend= array[sourceIndexPath.row];

//将该数据从数据源删除

[array removeObject:friend];
     //再找到新位置的数据源(destinationIndexPath是移动后的位置)
     NSMutableArray *array=self. dataSourceArray[destinationIndexPath.section];

//将数据先添加入数据源的新位置
     [array insertObject:friend atIndex:destinationIndexPath.row];

//最后刷新单元格
     [tableView reloadData];

}

时间: 2024-08-10 14:59:07

UITableViewCell单元格的删除、插入、移动的相关文章

iOS:多个单元格的删除(方法一)

采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们也要定义一个可变的数组,用来存储选中的数据,以便后来的删除.这里采用存储indexPath的方式,因为每选中一个单元格时,它都对应着一个indexPath,同时,将选中的单元格添加指引视图,即打对勾,也要判断打对勾是否重复,根据此来显示单元格的标记.最后,将标记的数据全部在原数据库中删除,同时在清空

iOS:多个单元格的删除(方法二):

前面介绍了万无一失的方法一,这里介绍删除单元格的第二种方式,通过删除单元格中的内容的方式进行操作:(但是这种情况有一个小的弊端,由于单元格重用机制,如果单元格内容一样时,标记的存在会造成误删) 删除前: 删除后: 分析如下:(如果每一个单元格内容都不一样)采取删除单元格内容的方式是比较简单的方式,那么如何实现多个单元格的删除呢? 首先,定义两个必要的可变的数组,一个是用来存储初始化原始数据的,另一个是用来存储选中单元格后,从里面取出来的数据: 其次,通过数据源的方法将原始数据显示在表格中,同时通

UITableViewCell 单元格样式

UITableViewCell 单元格样式作用 1 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { 2 UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x) 3 UITableViewCellStyleValue1, // Left ali

UITableView单元格不见删除按钮

现象:初学使用tableview的时候,进行单元格的删除操作,发现点击编辑的时候,或者滑动单元格的时候,左边的红色减号可以出来,但是右边的删除按钮却没有出来. 原因:这是一个低级错误,初学对布局不熟的时候,可能会犯,其实不是删除按钮没出来,而是删除按钮藏在屏幕之外了,不信你转动屏幕为横屏试试,说不定可以看到哦! 解放方法:将tableview的布局reset为建议约束.

HTML中Table行单元格的删除、增加、修改、确定

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

修改单元格——删除、插入、移动(IOS)

插入和删除时序: client: setEditing: animated: -----> 设定进入表视图 表视图---->委托: (<UITableViewDelegate>)tableView:editingStyleForRowAtIndexPath:方法进行单元格编辑图标的设置 方法进行单元格编辑图标的设置 表视图---->数据源:(<UITableViewDataSource>)tableView:commiEditingStyle:forRowAtIn

IOS之表视图单元格删除、移动及插入

1.实现单元格的删除,实现效果如下 Cpp代码   - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @"编辑"; self.navigation.rightBarButtonItem = self.editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view

UIKit框架(21)UITableView实现复杂单元格(一)

上篇文章介绍了UITableView的数据源驱动.重用机制.刷新数据等基本用法 本篇文章介绍如何实现一些复杂的单元格 UITableViewCell单元格对象有四种基本样式,开发中这个基本样式往往不能满足我们的需求,也就是说需要自定义UITableViewCell的样式,介绍主要的两种做法: 1)使用纯代码自定义 2)使用storyboard中的prototype cell 先来介绍一下UITableViewCell UITableViewCell表格单元格 UITableView中的单元格使用

Excel 中单元格和范围的引用(即访问的表示方法)

计算机中,无非是数据和数据的处理这两件事.Excel的工作表能存储大量数据,除了这些原始数据,我们还要用函数来处理这些数据,比如求和求积,求平均值,排序等等,并把处理结果也存在单元格里.在Excel中,我们通常不给这些数据起个专门的名字,比如StudentName1,StudentName2,Score1,Score2(我在另外一篇文章讲如何给单元格或范围内的数据命名),Excel本身已经隐含了指代这些变量的方式,不会让它们成为"失落的存储空间".一般我们在编程时,向函数传递的是我们程