iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记、移动、删除、插入。
要进行数据的操作了,把代码里的不可变数组改成可变的:
NSArray *list -》NSMutableArray *list
1、标记Cell。
效果如下:

打开项目,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
添加代码
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- // NSString *rowString = [self.list objectAtIndex:[indexPath row]];
- // UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- // [alter show];
- UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- if (cell.accessoryType == UITableViewCellAccessoryNone) {
- cell.accessoryType = UITableViewCellAccessoryCheckmark;
- }else {
- cell.accessoryType = UITableViewCellAccessoryNone;
- }
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
标记分别有四种效果:
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone
可以自己试试。
2、删除Cell
想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。
打开xib,生成Table的IBoutlet映射 tableView;
在viewDidload里添加
[self.tableViewsetEditing:YES];
这是启动运行程序,

打开可编辑模式,默认情况显示删除的图标的。
实现删除的代码:
- - (void)tableView:(UITableView *)tableView commitEditingStyle:
- (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- NSUInteger row = [indexPath row];
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- [self.list removeObjectAtIndex:row];
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
- withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- }
这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete
在这删除行的方法又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone
从常量名称打开可以看出效果来。
这是运行,就可以删除其中的一个Cell行了。
3、移动Cell
添加代码如下:
3.1先把默认的删除的图标去掉
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
- editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
- return UITableViewCellEditingStyleInsert;
- }
3.2返回当前Cell是否可以移动
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
- return YES;
- }
3.3执行移动操作
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
- sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
- NSUInteger fromRow = [sourceIndexPath row];
- NSUInteger toRow = [destinationIndexPath row];
- id object = [self.list objectAtIndex:fromRow];
- [self.list removeObjectAtIndex:fromRow];
- [self.list insertObject:object atIndex:toRow];
- }
运行程序:

怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动
4、插入cell:
4.1插入和删除差不多,在
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
添加UITableViewCellEditingStyleInsert判断
- - (void)tableView:(UITableView *)tableView commitEditingStyle:
- (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- NSUInteger row = [indexPath row];
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- [self.list removeObjectAtIndex:row];
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
- withRowAnimation:UITableViewRowAnimationAutomatic];
- }else if(editingStyle == UITableViewCellEditingStyleInsert ){
- NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
- [self.list insertObject:@"inset new Cell" atIndex:row];
- [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
- }
- }
4.2 修改图标为插入样式。
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
- editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
- return UITableViewCellEditingStyleInsert;
- }
运行,点加号图标,

完成!
例子代码:http://download.csdn.net/detail/totogo2010/4398669
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
iOS学习之UITableView中Cell的操作的更多相关文章
- UITableView中cell里的UITextField不被弹出键盘挡住
UITableView中cell里的UITextField不被弹出键盘挡住 本人视频教程系类 iOS中CALayer的使用 效果如下: 源码: EditCell.h 与 EditCell.m // ...
- UITableView中cell点击的绚丽动画效果
UITableView中cell点击的绚丽动画效果 本人视频教程系类 iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m / ...
- 如何获取UITableView中cell的frame值
如何获取UITableView中cell的frame值 这个可以用来处理UITableView弹出键盘的问题 本人视频教程系类 iOS中CALayer的使用 效果: 源码: // // ViewC ...
- 用适配器模式处理复杂的UITableView中cell的业务逻辑
用适配器模式处理复杂的UITableView中cell的业务逻辑 适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- iOS学习之UITableView
一.UITableView的概念 UITabelView继承于UIScrollView,可以滚动. @interface UITableView : UIScrollView <NSCoding ...
- iOS开发之UITableView中计时器的几种实现方式(NSTimer、DispatchSource、CADisplayLink)
最近工作比较忙,但是还是出来更新博客了.今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结.本篇博客会给出在TableV ...
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...
随机推荐
- BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*
BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur Description In an effort to better manage the grazing pat ...
- tableau-交互筛选器
欢迎观看这段关于“交互筛选器”的视频. 将字段拖到筛选器功能区就可以轻松执行筛选,但是如果我们希望能够直接从视图中修改筛选器 选择项,那该怎么办?如果我们在可看到“子类别”字段的任何位置右键单击, ...
- idea 创建Java WEB 项目
第一步 FILE - New Project 写上名字,然后 点 Finish 这里特别注意,不懂路径 那么 就点上 inherit 再点下面 让他默认 加载你的 web 路径 选择 Jar包 , ...
- 《DSP using MATLAB》第7章开始 Example7.1
FIR低通滤波器的设计要求示意图:
- 《DSP using MATLAB》示例 Example 6.25
代码: % x = [-0.9, 0.81]; [y, L, B] = QCoeff(x, 3) % Unquantized parameters r = 0.9; theta = pi/3; a1 ...
- ubuntu换系统下载路径源
ubuntn默认下载路径的源是国外的下载很慢换成国内的下载路径会好很多 在ubuntu应用搜索框(左上脚那个)中输入software 1.software&Updates 应用点击softwa ...
- Could not transfer artifact org.springframework:spring-tx:jar:3.2.3.RELEASE
在maven中加入依赖时,如果jar下载失败,这时pom文件中就会出现上面的错误,重新添加也不好用, 这时要手动去maven库中删除已经下载的依赖包. 默认库位置:C:\Users\XXX\.m2
- matlab 中的function定义. 用最简单的例子说明.
function y=myfunction(a,b)其中a,b是输入函数的参数,y是函数返回的值.当需要返回多个值时,可以将y看作一个数组,或者直接将函数的开头写成如function [x,y]=my ...
- 洛谷3354(IOI2005)河流——“承诺”
题目:https://www.luogu.org/problemnew/show/P3354 虽说是几个月前曾经讲过的题,但没有题解而自己(花了两个多小时)A了好高兴!!! 这是一个很好的套路:“承诺 ...
- new与malloc的区别,以及内存分配浅析
从函数声明上可以看出.malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小.比如: 1 2 3 int *p; p = new int; //返回类型 ...