Demo 介绍

86 阅读3分钟

功能介绍

  1. 国家列表展示
  2. 输入金额可以动态更新结果
  3. 添加国家页面的跳转
  4. 左滑删除国家

国家列表展示

相关的知识点

  1. UITableViewController
  2. 封装模型
  3. 加载数据

UITableViewController

  1. 界面的设计

    • 通过直接在storyboard上添加控件的方式
  2. 实现数据源方法

    • 因为没有分组,所以直接设置行数,而不设置组数
    //设置行数
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.countryArray.count;
    }
    
    //数据源方法
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ID = @"country";
        ZSCountryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        cell.country = self.countryArray[indexPath.row]; //赋值
        cell.delegate = self;
        return cell;
    }
    

封装模型

通过创建Country类来管理一个国家展示需要显示的数据

  • 在.m文件中定义相关的属性,比如国家名称,货币名称,输入金额等
  • 设置一个类方法countryWithDict:在该类方法中将等一下懒加载传递过来的值赋给刚刚定义的属性中去

通过创建一个CountryCell类来管理每一个行cell

  • 定义的属性是cell里控件的连线关联
  • 注意要在这里创建一个Country对象
  • 重写country的set方法,将country里保存的值传递给cell控件中去

加载数据

通过懒加载的方式

  1. 因为数据存放在plist文件中

    • plist文件的根是一个数组array类型
    • 数组中的每个元素是一个字典dictionary
    • 字典中存放着多个键值对,每个属性对应一个key,例如countryName-中国,moneyName-人民币¥
  2. 因此先定义一个数组countryArray来存放所有国家的数据

  3. 再在函数内创建一个数组dictArray,也是用于存放plist的所有国家数据

    通过arrayWithContentsOfFile方法将plist文件读入

NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Country.plist" ofType:nil]];
  1. 遍历字典数组(字典数组就是一个数组里的每个元素是字典),将每个字典数组通过上面定义的类方法countryWithDict将字典数组传给country这个类

输入金额可以动态更新结果

其实就是需要一个协议去监听负责输入金额的textField的数值变化,然后通过该数据计算得到其他国家的数据再显示到对应的textField中

自定义协议

在ZSCountryCell中定义协议,即cell是规定协议的人

//声明代理的协议方法
@protocol ZSCountryCellDelegate <NSObject>
-(void)countryCellDidEditTextField:(ZSCountryCell *)cell;
@end

//声明代理的属性
@property(nonatomic,weak)id<ZSCountryCellDelegate> delegate;

协议规定代理需要实现countryCellDidEditTextField

在ZSCountryCell中添加一个监听方法

当textfield发生改变时,去调用countryCellmoneyTextField方法

-(void)addTargetMethod{
    //当发生forControlEvents里的变化时,就去调用action里的函数
    //即 当UIControlEventEditingChanged时,就去调用countryCellmoneyTextField方法
    [self.moneyTextField addTarget:self action:@selector(countryCellmoneyTextField:) forControlEvents:UIControlEventEditingChanged];
}

countryCellmoneyTextField方法

//监听到后执行
-(void)countryCellmoneyTextField:(ZSCountryCell *)cell{
    //找代理去做事
    if(self.delegate && [self.delegate respondsToSelector:@selector(countryCellDidEditTextField:)]){
        [self.delegate countryCellDidEditTextField:self];
    }
}

其实就是去通知代理去执行countryCellDidEditTextField(也就是协议里的方法)

协议方法countryCellDidEditTextField

//实现代理协议的方法
-(void)countryCellDidEditTextField:(ZSCountryCell *)cell{
    //取得当前cell的indexpath
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    //用于存放需要刷新的行数
    NSMutableArray *testArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<self.countryArray.count; i++){
        //取出当前输入行的数组
        ZSCountry *country = self.countryArray[indexPath.row];
        country.moneyValue = cell.moneyTextField.text;
        
        if(i!=indexPath.row){ //如果不是被输入金额的那行,那么其他的即是需要进行汇率换算的部分
            ZSCountry *country = self.countryArray[i];
            //计算汇率换算的结果
            float result = [self exchangeRateConversion:cell.moneyTextField.text rowForCountry:indexPath.row order:i];
            country.moneyValue = [NSString stringWithFormat:@"%.2f",result];
            [testArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
    }
    [self.tableView reloadRowsAtIndexPaths:testArr withRowAnimation:UITableViewRowAnimationNone];
    
}

注意最后是通过reloadRowsAtIndexPaths的方法去刷新需要改变的cell

添加国家页面的跳转

添加国家页面的创建

在storyboard中添加一个UITableViewController

添加NavigationController

NavigationController的第一个界面就是上面的国家列表展示界面

再利用“添加”按钮让添加国家页面show,实现点击页面的跳转

左滑删除国家

//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.countryArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}