---
title: "新风作浪博客学习(十一)UITableViewCell的标记、移动、删除、插入 ."
author: "android_zhang"
date: "2013-06-06T09:15:06Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "uitableviewcell"
draft: false
lastmod: "2013-01-19T11:15:52Z"
id: 1772437
url: "https://android-zhang.iteye.com/blog/1772437"
---

这篇文章是建立在  代码实现 UITableView与UITableViewCell基础上进行修改，用不上的代码我注释调，部分不明白可以看看上篇博客；实现的功能是对UITableViewCell的标记、移动、删除、插入；


1.标记：指的是选中某一行，在这一行后面有个符号，常见的是对勾形式
通过修改cell的accessoryType属性来实现，首先，在ViewDidLoad中;表示把单元格可编辑状态这只为NO

```java
-(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   

![](http://dl.iteye.com/upload/attachment/0079/5205/ce85697e-6839-37b6-9616-75fae616b539.png)


UITableViewCellAccessoryDetailDisclosureButton

![](http://dl.iteye.com/upload/attachment/0079/5207/6d6fda26-66a0-389d-bf68-cd7e2e8b0fa0.png)


UITableViewCellAccessoryDisclosureIndicator

![](http://dl.iteye.com/upload/attachment/0079/5209/76f65150-4322-3254-81b6-42f2226725fd.png)



UITableViewCellAccessoryNone

![](http://dl.iteye.com/upload/attachment/0079/5211/606d8e15-189e-3ad0-88a4-2eb67fddb62a.png)









2.移动  
实现移动单元格就需要把单元格的编辑属性设置为YES，;

```java
//返回YES，表示支持单元格的移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
```



```java
//单元格返回的编辑风格，包括删除 添加 和 默认  和不可编辑三种风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}
```

三种风格的分别是：

UITableViewCellEditingStyleDelete   

![](http://dl.iteye.com/upload/attachment/0079/5213/f9e616c5-b61e-3709-beca-f732081b0292.png)



UITableViewCellEditingStyleInsert

![](http://dl.iteye.com/upload/attachment/0079/5215/bbffeaca-9c7e-39fa-8af0-89a0ebed11e5.png)



UITableViewCellEditingStyleNone

![](http://dl.iteye.com/upload/attachment/0079/5217/63509eb1-39f1-3f86-9c2b-f8addb67c5e1.png)


实现移动的方法

```java
-(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，所以

```java
//单元格返回的编辑风格，包括删除 添加 和 默认  和不可编辑三种风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
```



```java
-(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];
    }
}
```


删除了张四 效果图：

![](http://dl.iteye.com/upload/attachment/0079/5219/d2afa52f-54e3-3d67-8a58-a121fc817047.png)




4.添加
实现方法和删除方法相同，首先还是返回单元格编辑风格

```java
//单元格返回的编辑风格，包括删除 添加 和 默认  和不可编辑三种风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
}
```



为了显示效果明显，在.h文件中声明一个变量i

```java
#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
```



```java
-(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];
    }
}
```


运行效果图:

![](http://dl.iteye.com/upload/attachment/0079/5221/9758afbc-6608-3e4a-8c6c-e406563f41e8.png)


在删除和添加单元格的用到UITableViewRowAnimation动画效果，它还有其他几种效果，在此不做测试
UITableViewRowAnimationAutomatic      UITableViewRowAnimationTop 

UITableViewRowAnimationBottom          UITableViewRowAnimationLeft

UITableViewRowAnimationRight             UITableViewRowAnimationMiddle

UITableViewRowAnimationFade              UITableViewRowAnimationNone