接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记、移动、删除、插入。

为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http://download.csdn.net/detail/totogo2010/4361870

要进行数据的操作了,把代码里的不可变数组改成可变的:

NSArray *list -》NSMutableArray *list

1、标记Cell。

效果如下:

打开项目,

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。

添加代码

  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2. //    NSString *rowString = [self.list objectAtIndex:[indexPath row]];
  3. //    UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  4. //    [alter show];
  5. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  6. if (cell.accessoryType == UITableViewCellAccessoryNone) {
  7. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  8. }else {
  9. cell.accessoryType = UITableViewCellAccessoryNone;
  10. }
  11. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  12. }

标记分别有四种效果:

UITableViewCellAccessoryCheckmark

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryNone

可以自己试试。

2、删除Cell

想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。

打开xib,生成Table的IBoutlet映射  tableView;

在viewDidload里添加

[self.tableViewsetEditing:YES];

这是启动运行程序,

打开可编辑模式,默认情况显示删除的图标的。

实现删除的代码:

  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:
  2. (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  3. NSUInteger row = [indexPath row];
  4. if (editingStyle == UITableViewCellEditingStyleDelete) {
  5. [self.list removeObjectAtIndex:row];
  6. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  7. withRowAnimation:UITableViewRowAnimationAutomatic];
  8. }
  9. }

这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete

在这删除行的方法又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:

UITableViewRowAnimationAutomatic

UITableViewRowAnimationTop

UITableViewRowAnimationBottom

UITableViewRowAnimationLeft

UITableViewRowAnimationRight

UITableViewRowAnimationMiddle

UITableViewRowAnimationFade

UITableViewRowAnimationNone

从常量名称打开可以看出效果来。

这是运行,就可以删除其中的一个Cell行了。

3、移动Cell

添加代码如下:

3.1先把默认的删除的图标去掉

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
  2. editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  3. return UITableViewCellEditingStyleInsert;
  4. }

3.2返回当前Cell是否可以移动

  1. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  2. return YES;
  3. }

3.3执行移动操作

  1. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
  2. sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
  3. NSUInteger fromRow = [sourceIndexPath row];
  4. NSUInteger toRow = [destinationIndexPath row];
  5. id object = [self.list objectAtIndex:fromRow];
  6. [self.list removeObjectAtIndex:fromRow];
  7. [self.list insertObject:object atIndex:toRow];
  8. }

运行程序:

怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动

4、插入cell:

4.1插入和删除差不多,在

- (void)tableView:(UITableView *)tableView commitEditingStyle:

(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

添加UITableViewCellEditingStyleInsert判断

  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:
  2. (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  3. NSUInteger row = [indexPath row];
  4. if (editingStyle == UITableViewCellEditingStyleDelete) {
  5. [self.list removeObjectAtIndex:row];
  6. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  7. withRowAnimation:UITableViewRowAnimationAutomatic];
  8. }else if(editingStyle == UITableViewCellEditingStyleInsert ){
  9. NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
  10. [self.list insertObject:@"inset new Cell" atIndex:row];
  11. [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
  12. }
  13. }

4.2 修改图标为插入样式。

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
  2. editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  3. return UITableViewCellEditingStyleInsert;
  4. }

运行,点加号图标,

完成!

例子代码:http://download.csdn.net/detail/totogo2010/4398669

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

iOS学习之UITableView中Cell的操作的更多相关文章

  1. UITableView中cell里的UITextField不被弹出键盘挡住

    UITableView中cell里的UITextField不被弹出键盘挡住 本人视频教程系类   iOS中CALayer的使用 效果如下: 源码: EditCell.h 与 EditCell.m // ...

  2. UITableView中cell点击的绚丽动画效果

    UITableView中cell点击的绚丽动画效果 本人视频教程系类   iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m / ...

  3. 如何获取UITableView中cell的frame值

    如何获取UITableView中cell的frame值 这个可以用来处理UITableView弹出键盘的问题 本人视频教程系类   iOS中CALayer的使用 效果: 源码: // // ViewC ...

  4. 用适配器模式处理复杂的UITableView中cell的业务逻辑

    用适配器模式处理复杂的UITableView中cell的业务逻辑 适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell ...

  5. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  6. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  7. iOS学习之UITableView

    一.UITableView的概念 UITabelView继承于UIScrollView,可以滚动. @interface UITableView : UIScrollView <NSCoding ...

  8. iOS开发之UITableView中计时器的几种实现方式(NSTimer、DispatchSource、CADisplayLink)

    最近工作比较忙,但是还是出来更新博客了.今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结.本篇博客会给出在TableV ...

  9. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...

随机推荐

  1. BZOJ5369 [Pkusc2018]最大前缀和

    题意 小C是一个算法竞赛爱好者,有一天小C遇到了一个非常难的问题:求一个序列的最大子段和. 但是小C并不会做这个题,于是小C决定把序列随机打乱,然后取序列的最大前缀和作为答案. 小C是一个非常有自知之 ...

  2. Numpy, Pandas, Matplotlib, Scipy 初步

    Numpy: 计算基础,  以类似于matlab的矩阵计算为基础.  底层以C实现, 速度快. Pandas: 以numpy为基础, 扩充了很多统计工具. 重点是数据统计分析. Matplotlib: ...

  3. 自定义vue全局组件use使用(解释vue.use()的原理)

    我们在前面学习到是用别人的组件:Vue.use(VueRouter).Vue.use(Mint)等等.其实使用的这些都是全剧组件,这里我们就来讲解一下怎么样定义一个全局组件,并解释vue.use()的 ...

  4. java之反射概述

    类加载器和反射  类加载器: 1 类的加载过程: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步骤来实现对这个类进行初始化. 加载:就是指将class文件读入内存 ...

  5. python编程规范系列--建议01~07

    本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容     1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等:     2)编写py ...

  6. webservice-之使用axis+spring开发

    一.环境配置 :在 eclipse 中配置引入相应的 spring框架( core/Remoting/Web ). axis 包.   二.代码开发 1.  在 MyEclipse 中建立一个新的 J ...

  7. angular的路由跳转,的监听$rootScope.$on

    使用angular来做项目时,习惯性的使用第三方路由插件ui-router配置路由.每一个状态都对应着一个页面, 因此对路由状态改变的监听也变的十分重要. 可以使用:$rootScope.$on(…… ...

  8. Editplus配置Java、Python、C/C++ (基于VS2010) 编译环境

    1. 为什么要配置EditPlus使其能够编译运行Java.Python.C/C++等程序? EditPlus是一款轻量级(大约2M)的文本编辑器,实际开发中,只需要关联相应的编译工具就可以化身为一个 ...

  9. 类的声明与实例化及构造方法析构方法(PHP学习)

    <?php class human{ public static $leg=2; public $name = 'leo'; public $age = '25'; public functio ...

  10. CAsyncSocket只传输了一部分数据(UDP),后面是乱码

    void CCAsyncSocketDlg::OnBnClickedBtnSend() { UpdateData(TRUE); TCHAR ipstr[INET_ADDRSTRLEN]; DWORD ...