天天看點

plist檔案的讀取和tableView的編輯(插入和删除)

這裡直接上代碼執行個體,注意需要服從兩個代理:UITableViewDataSource,UITableViewDelegate,這裡讀取的plist檔案是 字典-數組 類型的.h檔案裡代碼

#import <UIKit/UIKit.h>
#import "ContentView.h"
//1.遵守協定
@interface RootViewController : UIViewController<uitableviewdatasource uitableviewdelegate="">

@property(nonatomic,retain)ContentView *contentView;

@property(nonatomic,retain)NSMutableDictionary *allDataDic;//存放的是所有資訊

@end
</uitableviewdatasource></uikit>
           

.m檔案裡代碼

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //1.擷取plist檔案路徑
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Citys" ofType:@".plist"];
    //2.根據檔案路徑讀取檔案内容
    //字典類型
    self.allDataDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    //數組類型
//    [NSMutableArray arrayWithContentsOfFile:filePath];
    //3.周遊字典
    for (int i=0; i< _allDataDic.allKeys.count; i++) {
        //4.擷取每個key
        NSString *key = [_allDataDic.allKeys objectAtIndex:i];
        //5.通過key找到對應的數組
        NSArray *array = [_allDataDic objectForKey:key];
        //6.周遊小數組,并輸出其中每個元素
        for (int j=0; j<array.count; j++) {
            NSLog(@"%@",array[j]);
        }
    }
    
    //2.設定代理
    _contentView.tableView.delegate = self;
    _contentView.tableView.dataSource = self;
    
    //在導航控制器上添加一個編輯按鈕(系統自帶的)
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

#pragma mark------tableView編輯(删除,插入)
//1.編輯第一步:讓tableView處于可以編輯狀态
//此setEditing:animated: 方法是允許viewController上的所有的視圖可以處于編輯裝備
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //讓tableView可以處于編輯狀态
    [_contentView.tableView setEditing:editing animated:YES];
}
//2.編輯第二步:設定指定分區(section)中的行(row)是否可以被編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return NO;
    }
    return YES;
}
//3.編輯第三步:設定指定分區(section)中的行(row)是什麼編輯樣式(删除,插入)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return UITableViewCellEditingStyleInsert;//插入樣式
    }
    return UITableViewCellEditingStyleDelete;//删除樣式
}
//4.編輯第四步:編輯完成(先操作資料源(M層),再修改UI(V層))
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        
        //同步更新beginUpdates和endUpdates之間修改的UI和資料
        [_contentView.tableView beginUpdates];
        
        //①.删除資料
        NSString *key = [_allDataDic.allKeys objectAtIndex:indexPath.section];
        NSMutableArray *array = [_allDataDic objectForKey:key];
        [array removeObjectAtIndex:indexPath.row];
        //②.删除UI
        [_contentView.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationLeft];
        if (array.count == 0) {
            //删除區頭的資料(删除key對應的小數組)
            NSString * key = _allDataDic.allKeys[indexPath.section];
            [_allDataDic removeObjectForKey:key];
            
            //當某個區中僅剩一個cell的時候(或者小數組中隻剩一個資料的時候),删除時也要把區頭删掉
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
            [_contentView.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationLeft];
        }
        
        [_contentView.tableView endUpdates];
        
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        NSLog(@"插入");
        //①.插入資料
        NSString *key = _allDataDic.allKeys[indexPath.section];
        NSMutableArray *array = [_allDataDic objectForKey:key];
        [array insertObject:@"昌平區" atIndex:indexPath.row];
        //②.插入UI(cell)
//        NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [_contentView.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
#pragma mark------tableView的移動
//移動第一步:設定tableView是否處以可編輯狀态
//(同上)setEidting:Animation:

//移動第二步:設定指定的行是否可以移動
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;//所有行都可以移動
}

//移動第三步:實作移動
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //擷取數組中要移動的元素
    //1.擷取區
    NSString *key = _allDataDic.allKeys[sourceIndexPath.section];
    //2.通過key擷取對應的小數組
    NSMutableArray *array = [_allDataDic objectForKey:key];
    //3.擷取小數組中的每個城市名
    NSString *cityName = [array objectAtIndex:sourceIndexPath.row];
    //4.擷取的城市名進行儲存,給cityName指向的對象引用計數+1,防止野指針出現
    [cityName retain];
    //5.删除數組中的字元串
    [array removeObjectAtIndex:sourceIndexPath.row];
    //6.将要移動的元素,移動到destinationIndexPath.row那個位置上
    [array insertObject:cityName atIndex:destinationIndexPath.row];
    //7.遵循記憶體管理的黃金法則,這個release是對應上面的那個retain
    [cityName release];
    
}
//移動第四步:限制跨區移動
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    NSLog(@"source = %@",sourceIndexPath);
    NSLog(@"destination = %@",proposedDestinationIndexPath);
    
    //如果移動的位置屬于同一個區時可以移動,不是同一個區則傳回原來位置
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    return sourceIndexPath;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消關注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"讨厭,你點了取消關注...");
    }];
    UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"别亂删,小心找不回來....");
    }];
    NSArray *array = @[rowAction1,rowAction2];
    return array;
}



#pragma mark------實作協定中的相關方法
//設定有多少個分區
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _allDataDic.allKeys.count;
}

//設定某個分區有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //擷取每個分區(key)
    NSString *key = [_allDataDic.allKeys objectAtIndex:section];
    //根據key擷取數組
    NSArray *array = [_allDataDic objectForKey:key];//或者_allDataDic[key];
    
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //首先到集合中取cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    //如果沒有相應的cell,就建立一個
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"]autorelease];
    }
    //設定cell的顯示内容
    //1.擷取key
    NSString *key = [_allDataDic.allKeys objectAtIndex:indexPath.section];
    //2.根據key擷取小數組
    NSArray *array = _allDataDic[key];
    //3.擷取小數組中的城市名
    NSString *cityName = array[indexPath.row];
    //4.顯示在cell上
    cell.textLabel.text = cityName;
    //5.傳回設定好的cell
    return cell;
}

#pragma mark-----設定區頭标題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[_allDataDic allKeys]objectAtIndex:section];
}
           

還需要定義一個繼承與UIView的類,這裡是ContentView.h檔案裡代碼

#import <UIKit/UIKit.h>

@interface ContentView : UIView

@property(nonatomic,retain)UITableView *tableView;

@end
</uikit>
           

.m檔案裡的代碼為

-(void)dealloc
{
    [self.tableView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        [self addSubview:_tableView];
        [_tableView release];
    }
    return self;
}
           

plist檔案裡的内容為

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>北京</key>
    <array>
        <string>海澱區</string>
        <string>朝陽區</string>
        <string>東城區</string>
        <string>西城區</string>
        <string>通州區</string>
    </array>
    <key>河南</key>
    <array>
        <string>洛陽市</string>
        <string>開封市</string>
        <string>鄭州市</string>
        <string>安陽市</string>
    </array>
    <key>新疆</key>
    <array>
        <string>烏魯木齊</string>
        <string>吐魯番</string>
        <string>伊犁</string>
    </array>
    <key>上海</key>
    <array>
        <string>寶山區</string>
        <string>嘉定區</string>
        <string>徐彙區</string>
        <string>虹橋區</string>
        <string>黃浦區</string>
    </array>
</dict>
</plist>