---
title: "表UITableViewController 的一些操作方法"
author: "helmsman_xcode"
date: "2011-08-15T16:47:33Z"
tags: []
categories: ["移动开发"]
slug: "uitableviewcontroller"
draft: false
lastmod: "2011-08-15T16:58:14Z"
id: 1149704
url: "https://helmsman-xcode.iteye.com/blog/1149704"
---

是否可以对表进行编辑

```
[self.tableView setEditing:BOOL animated:YES];
```

 

BOOL 为YES 可对表中的数据进行如下操作：

移动

 

```
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSUInteger fromRow = [sourceIndexPath row];
    NSUInteger toRow = [destinationIndexPath row];
    
    id object = [[listOne objectAtIndex:fromRow] retain];
    
    [listOne removeObjectAtIndex:fromRow];
    [listOne insertObject:object atIndex:toRow];
    
}
```

 

删除

 

```
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    [self.listOne removeObjectAtIndex:row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
```

 

对表进行添加操作以后就表进行更新

 

```
 [self.tableView reloadData];
```