#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain)NSMutableArray *firstArray;
@property (nonatomic, retain)NSMutableArray *secondArray;
@property (nonatomic, retain) UITableView *tableView;
@end
@implementation RootViewController
記得釋放
- (void)dealloc
{
[_firstArray release];
[_secondArray release];
[_tableView release];
[super dealloc];
}
別忘記調用方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpData];
[self addTableView];
[self addBarButtonItem];
}
進入正題
// 添加數據源
- (void)setUpData
{
NSArray *tempArray = @[@"0",@"1",@"2",@"3",@"添加"];
// 初始化 兩個可變數組源數組
self.firstArray = [NSMutableArray arrayWithArray:tempArray];
self.secondArray = [NSMutableArray arrayWithArray:tempArray];
}
#pragma mark - 創建tableView
- (void)addTableView
{
self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
[_tableView release];
}
#pragma mark - 兩個重要的數組源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0) {
return self.firstArray.count;
}else {
return self.secondArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
// 顯示數據 先判斷分區
// 再用行數 作爲數組的索引 取出數據
if (indexPath.section == 0) {
cell.textLabel.text = self.firstArray[indexPath.row];
}else {
cell.textLabel.text = self.secondArray[indexPath.row];
}
return cell;
}
#pragma mark - 返回分區數
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"第一分區";
}else {
return @"第二分區";
}
}
#pragma mark - 添加右按鈕
- (void)addBarButtonItem
{
self.navigationItem.title = @"主界面";
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"編輯" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRightButton:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
UITableView 編輯兩步取:
1. 對UI界面進行操作
2. 對數據源數據進行操作 (防止刷新界面後 界面沒有變化)
UITableView編輯步驟:
1. 讓UITableView成爲 可編輯狀態
2. 返回(指定)可以被編輯的分區的那行 默認是都能編輯
3. 返回(指定)那個分區或那行的編輯樣式 添加or刪除
4. 完成編輯 根據編輯樣式 完成不同的操作 操作大體分兩步: (1)更改數據源數據 (2)刷新頁面
// 編輯觸發按鈕方法
// 1.開啓編輯狀態
- (void)actionRightButton:(UIBarButtonItem *)rightButton
{
// 開啓 UITableView 編輯狀態
// self.tableView.editing 默認是NO的
[self.tableView setEditing:!self.tableView.editing animated:YES];
// 更改編輯按鈕標題
if (self.tableView.editing == YES) {
rightButton.title = @"完成";
// self.navigationItem.rightBarButtonItem.title = @"完成";
}else {
rightButton.title = @"編輯";
// self.navigationItem.rightBarButtonItem.title = @"編輯";
}
}
// 2.哪個indexPath(那個分區的那行) 可以被編輯
// 如果你不寫 默認是YES的
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.section == 0 && indexPath.row == 1) {
// return NO;
// }
return YES;
}
// 3. 指定被編輯的樣式 添加or刪除
// 返回哪個分區的那行的 編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 判斷分區
if (indexPath.section == 0) {
// 第一分區
// 再判斷數組中的數據, 只讓添加字樣 返回添加
if([self.firstArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
}else {
// 第二分區
if ([self.secondArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
}
return UITableViewCellEditingStyleDelete;
}
// 4. 按編輯樣式 提交那個分區的那行的結果 完成編輯
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 更新數據 刷新界面
// 先判斷分區
if (indexPath.section == 0) {
// 第一分區
// 在判斷編譯樣式
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 刪除
// 利用indexPath.row 刪除數組中想對應的數據
[self.firstArray removeObjectAtIndex:indexPath.row];
// 刷新界面
// 下面方法 用在刪除數據刷新界面
// 需要一個數組 數組中是刪除的索引
// 這個數組 可以是多行的索引
[self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationRight)];
} else {
// 添加
// 更改數據
[self.firstArray insertObject:@"123" atIndex:indexPath.row];
// 刷新界面
// 下面方法 用在插入數據時 刷新界面
// 這個方法 是對行進行的刷新
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}
}else{
// 第二分區
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.secondArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}else {
[self.secondArray insertObject:@"234" atIndex:indexPath.row];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}
}
}
UITableView 移動步驟
1.讓UITableView成爲 可編輯狀態
2.指定 那個分區的那行 可以被移動
3.完成移動後 更新數據 刷新界面
// 1.UITableView 可編輯狀態(已完成)
// 2.指定 那行那分區 可編輯
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.section == 0) {
// return NO;
// }
return YES;
}
#pragma mark - 移動完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// sourceIndexPath 來源的索引(你開始拿起來cell的位置)
// destinationIndexPath 目的地的索引 (放下的cell的位置)
// 分兩種情況
// 1.同區移動 2.跨區移動
if (sourceIndexPath.section == destinationIndexPath.section) {
// 同區
if(sourceIndexPath.section == 0) {
// NSLog(@"%ld %ld", sourceIndexPath.row, destinationIndexPath.row);
// 操作第一分區數組
// 先保存一下 來源索引處的數據
NSString *str = self.firstArray [sourceIndexPath.row];
// 在從數組中 按來源索引刪除該數據
[self.firstArray removeObjectAtIndex:sourceIndexPath.row];
// 最後 把保存的數據 插入到目的地的地索引處
[self.firstArray insertObject:str atIndex:destinationIndexPath.row];
// 刷新界面(移動刷新方法 從那來的去那)
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}else {
//操作第二區分組
NSString *str = self.secondArray [sourceIndexPath.row];
// 在從數組中 按來源索引刪除該數據
[self.secondArray removeObjectAtIndex:sourceIndexPath.row];
// 最後 把保存的數據 插入到目的地的地索引處
[self.secondArray insertObject:str atIndex:destinationIndexPath.row];
// 刷新界面(移動刷新方法 從那來的去那)
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
限制跨區移動
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// sourceIndexPath 來源索引
// proposedDestinationIndexPath 推薦的目的地索引
// 只要你拖動 就會觸發這個方法
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}else {
return sourceIndexPath;
}
}