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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值