1、tableView的編輯的步驟:
1.讓tableView處于編輯狀态,(預設所有的cell都處于編輯狀态,預設下的編輯樣式是删除)
2.設定哪些cell可以編輯
3.設定編輯的樣式(删除,插入)
4.送出編輯結果(先修改資料源,再修改UI)
tableView的移動的步驟:
1.讓tableView處于編輯狀态
2.設定哪些cell可以移動
3.送出移動結果

————————————————————————————————
AppDelegate.m
self.window.rootViewController
=
[[[UINavigationController
alloc]initWithRootViewController:[RootViewController
alloc]]autorelease];
RootViewController.m
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController
()<</span>UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableDictionary
*dict;
@property(nonatomic,retain)NSMutableArray *orderKeys;
@end
@implementation RootViewController
-(void)dealloc{
self.dict = nil;
self.orderKeys = nil;
[super
dealloc];
}
重寫loadView方法,将UITableView指定為視圖控制器的對象
-
(void)loadView{
UITableView
*tableView =
[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
//設定資料源代理
tableView.dataSource = self;
//設定業務代理
tableView.delegate = self;
//将tableView 指定為rootViewController
根視圖
self.view = tableView;
[tableView release];
(void)viewDidLoad {
viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
配置導覽列的方法
[self
configureNavigationContent];
從plist檔案讀取資料
readDataFromPlist];
從plist檔案讀取資料
(void)readDataFromPlist{
1.擷取檔案路徑
NSString
*filePath =
[[NSBundle mainBundle]pathForResource:@"Contacts.plist"
ofType:nil];
2.根據檔案路徑初始化字典對象,因為此時檔案的最外層是字典
self.dict = [NSMutableDictionary
dictionaryWithContentsOfFile:filePath];
//
NSLog(@"%@",diict);//驗證是否取出來
copy一個一摸一樣的字典出來,枚舉copy出來的字典,修改原來的字典
NSDictionary
*copyDict =
[NSDictionary dictionaryWithDictionary:self.dict];
周遊字典
for (NSString *key in copyDict) {
NSArray
*array =
copyDict[key];
//初始化可變數組
NSMutableArray
*mArr =
[NSMutableArray arrayWithArray:array];
[self.dict setValue:mArr forKey:key];
}
//3.擷取字典中所有的key值
*keys
= self.dict.allKeys;
//4.對數組中keys排序
NSArray *array =
[keys sortedArrayUsingSelector:@selector(compare:)];
//5.初始化存放key的數組
self.orderKeys = [NSMutableArray arrayWithArray:array];
//配置導覽列的顯示的内容
- (void)configureNavigationContent{
self.navigationItem.title =
@"通訊錄";
self.navigationController.navigationBar.barTintColor
= [UIColor orangeColor];
//設定編輯按鈕
self.navigationItem.rightBarButtonItem
= self.editButtonItem;
//重寫點選Edit按鈕方法
- (void)setEditing:(BOOL)editing
animated:(BOOL)animated{
setEditing:editing animated:animated];
NSLog(@"%d",editing);
驗證可編輯狀态
editing
為1時可以編輯,為0時不可以編輯
[(UITableView
*)self.view
setEditing:editing animated:YES ];
——————————————————————————————————
#pragma mark
必須實作的資料源代理方法
//2.配置哪些cell可以編輯
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section < 3) {
//
return YES;
return NO;
return
indexPath.section <</span> 3 ? YES : NO;
//設定deligt為删除
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath{
@"删除";
//4.送出編輯操作
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
//先修改資料源再更新UI(界面)
//1.根據分區索引擷取key值,确定要删除的cell在哪個分區(eg:B分區
D分區)
NSString
*key
= self.orderKeys[indexPath.section];
//2.根據key值拿到字典中對應的分組
*group =
self.dict[key];
//删除
if (editingStyle ==
UITableViewCellEditingStyleDelete)
{
//處理删除操作
if (1 == group.count) {//删除整個分組
//1.先删除資料源,從字典中移除key值
[self.dict removeObjectForKey:key];
//删除索引欄數組中的對應的元素
[self.orderKeys removeObjectAtIndex:indexPath.section];
//2.更新UI界面
//建立一個NSIndexSex
對象,使用分區下标初始化
NSIndexSet
*indexSet =
[NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:indexSet withRowAnimation:(UITableViewRowAnimationLeft)];
}else{//删除對應的cell即可
//先删除資料源
[group removeObjectAtIndex:indexPath.row];
//再更新UI界面
//tableView删除是可以删除多行
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:(UITableViewRowAnimationLeft)];
}else{//添加
//1.準備要插入的資料
*dic =
@{@"name":@"黃凱",@"gender":@"妖",@"age":@"25",@"phone":@"3838438",@"imageName":@"1.png",@"says":@"千人斬"};
//2.修改資料源
[group insertObject:dic atIndex:indexPath.row];
//3.更行UI界面
[tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:(UITableViewRowAnimationRight)];
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView{
//根據字典中鍵值對的個數傳回二分區個數
self.dict.count;
//傳回row個數
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
//1.擷取對應分區下标所對應的key值
*key =
self.orderKeys[section];
//2.根據key值取出字典的value值并且傳回數組元素的個數
[self.dict[key]count];
//傳回區頭标題
titleForHeaderInSection:(NSInteger)section{
//傳回對應分區區頭
//傳回右側索引欄
- (NSArray *)sectionIndexTitlesForTableView:(UITableView
self.orderKeys;
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.重建重用id标示
static
*identifier =
@"cell";
//2.tableView對象去重用池取可以重用的cell
UITableViewCell *cell
=
[tableView dequeueReusableCellWithIdentifier:identifier];
//3.判斷有沒有取到cell
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle)
reuseIdentifier:identifier]autorelease];
//展示資料步驟:
//1.根據cell所在分區的索引擷取對應的key值
self.orderKeys[indexPath.section];
//2.根據key值擷取字典中的value值
*values =
//3.根據row的索引擷取數組中對應的元素
*pDict =
values[indexPath.row];
NSLog(@"%@",pDict);//驗證是否取得字典中的人資訊
//4.取出字典中資料用cell展示
cell.imageView.image = [UIImage imageNamed:pDict[@"imageName"]];
cell.textLabel.text = pDict[@"name"];
cell.detailTextLabel.text = pDict[@"phone"];
cell;
#pragma mark
tableView 的移動
//設定哪些行可以移動
canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
YES;
//送出移動後的操作
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath{
sourceIndexPath cell原來的位置
destinationIndexPath
cell移動之後的位置
//
移動操作不需要更新UI界面,因為移動的時候UI界面已經發生了變化,此時隻需修改資料源即可
//首先擷取cell展示的資料所在的數組
//1.取出key值
self.orderKeys[sourceIndexPath.section];
//2.取出字典中key值對應的數組
*mArr
= self.dict[key];
//3.将原來的資料取出來一份儲存起來
[mArr[sourceIndexPath.row]retain];//2
//4.删除數組中原來位置的元素
[mArr
removeObjectAtIndex:sourceIndexPath.row];//1
//5.将元素插入到數組中新的位置
insertObject:dic atIndex:destinationIndexPath.row];//2
//釋放dic
[dic
release];//1
//如果移動過後不是在原來的分區,則取消移動結果傳回原來位置
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
proposedDestinationIndexPath
//如果在同一分區則讓cell移動,傳回移動後的位置
if (sourceIndexPath.section
==
proposedDestinationIndexPath.section) {
proposedDestinationIndexPath;
}else{//如果不在同一分區,傳回移動之前的位置
sourceIndexPath;
——————————————————————-——————————
業務代理方法的實作
//3.設定tableView的編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView
*)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section < 2) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleInsert;
indexPath.section <</span> 2 ? UITableViewCellEditingStyleDelete
: UITableViewCellEditingStyleInsert;
//設定行高
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
80.0;
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController
*detailVC =
[[DetailViewController
alloc]init];
//取出數組中的字典并指派給屬性
mArr[indexPath.row];
detailVC.dic = dic;
[self.navigationController
pushViewController:detailVC
animated:YES];
[detailVC release];
=======================================================
DetailViewController.h
@property(nonatomic,retain)NSDictionary *dic;
DetailViewController.m
(void)dealloc{
self.dic = nil;
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
[(DetailView *)self.view assignAllController:self.dic];
- (void)loadView{
DetailView
*detaileView =
[[DetailView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = detaileView;
[detaileView release];
最終效果:

===================================================
傳值不做介紹,僅供參考!
歡迎學習本文,未經許可,禁止轉載!