AJ分享,必須精品
### 先看效果圖




### 代碼
~~~
//
// Created by apple on 14-8-19.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "HMViewController.h"
@interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
/** 數據列表 */
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation HMViewController
- (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
return _tableView;
}
- (NSMutableArray *)dataList
{
if (_dataList == nil) {
_dataList = [NSMutableArray arrayWithObjects:@"貓貓1號", @"貓貓1號", @"貓貓2號", @"貓貓3號", @"貓貓4號", @"貓貓5號",@"貓貓6號", @"貓貓7號", @"貓貓8號",@"貓貓9號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",@"貓貓1號", @"貓貓1號", @"貓貓1號",nil];
}
return _dataList;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tableView];
// 開始編輯,一旦editing == YES就默認開啟刪除模式
self.tableView.editing = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 設置表格
cell.textLabel.text = self.dataList[indexPath.row];
return cell;
}
// 只要實現了此方法,就能夠支持手勢拖拽刪除了,刪除需要自己干!
/**
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete, 刪除
UITableViewCellEditingStyleInsert 添加
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"要刪除");
// MVC => 數據是保存在模型中
// 1. 刪除self.dataList中indexPath對應的數據
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList);
// 2. 刷新表格(重新加載數據)
// 重新加載所有數據
// [self.tableView reloadData];
// deleteRowsAtIndexPaths讓表格控件動畫刪除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加數據");
// 1. 向數組添加數據
[self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths讓表格控件動畫在指定indexPath添加指定行
// 新建一個indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
// 只要實現此方法,就可以顯示拖動控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 界面數據UITableView已經完成了
// 調整數據即可
// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
// 1. 將源從數組中取出
id source = self.dataList[sourceIndexPath.row];
// 2. 將源從數組中刪除
[self.dataList removeObjectAtIndex:sourceIndexPath.row];
NSLog(@"%@", self.dataList);
// 3. 將源插入到數組中的目標位置
[self.dataList insertObject:source atIndex:destinationIndexPath.row];
NSLog(@"%@", self.dataList);
}
#pragma mark - 代理方法
// 返回編輯樣式,如果沒有實現此方法,默認都是刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
@end
~~~
### UITableView支持刪除手勢
只要實現了此方法,就能夠支持手勢拖拽刪除了,刪除需要自己干!
~~~
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
~~~
UITableViewCellEditingStyleNone, 沒效果
UITableViewCellEditingStyleDelete, 刪除
UITableViewCellEditingStyleInsert 添加
刪除中要做的:
重新加載數據時候用[self.tableView reloadData];會效率低下
~~~
// MVC => 數據是保存在模型中
// 1. 刪除self.dataList中indexPath對應的數據
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList);
// 2. 刷新表格(重新加載數據)
// 重新加載所有數據
// [self.tableView reloadData];
// deleteRowsAtIndexPaths讓表格控件動畫刪除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
~~~
### UITableView 增加
要實現守代理方法 tableView.delegate = self;
~~~
// 返回編輯樣式,如果沒有實現此方法,默認都是刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
~~~
下面是在- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 方法中設置的
判斷編輯樣式,是刪除還是增添,然后做相應操作
~~~
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加數據");
// 1. 向數組添加數據
[self.dataList insertObject:@"旺旺旺旺狗狗" atIndex:indexPath.row + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths讓表格控件動畫在指定indexPath添加指定行
// 新建一個indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
~~~
### UITableView 移動
~~~
// 只要實現此方法,就可以顯示拖動控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 界面數據UITableView已經完成了
// 調整數據即可
// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
// 1. 將源從數組中取出
id source = self.dataList[sourceIndexPath.row];
// 2. 將源從數組中刪除
[self.dataList removeObjectAtIndex:sourceIndexPath.row];
NSLog(@"%@", self.dataList);
// 3. 將源插入到數組中的目標位置
[self.dataList insertObject:source atIndex:destinationIndexPath.row];
NSLog(@"%@", self.dataList);
}
~~~
?