zoukankan      html  css  js  c++  java
  • UITableView(三)

      1 #import "ViewController.h"
      2 #import "CityList.h"
      3 #import "FirstTableViewController.h"
      4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
      5 @property(nonatomic,strong)  UITableView *tableView ;
      6 /**
      7  *  设置存储城市对象的字典
      8  */
      9 @property(nonatomic,strong)NSMutableDictionary *allCityDic;
     10 @property(nonatomic,strong)NSMutableArray *keysArray;
     11 @end
     12 
     13 @implementation ViewController
     14 
     15 - (void)viewDidLoad {
     16     [super viewDidLoad];
     17     // Do any additional setup after loading the view, typically from a nib.
     18     //创建表视图
     19     UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
     20     tableView.dataSource = self;
     21     tableView.delegate = self;
     22     [self.view addSubview:tableView];
     23     self.tableView = tableView;
     24     //读取plist中的数据
     25     [self readPlist];
     26     
     27 //  [self.tableView setEditing:YES animated:YES];
     28     
     29     //添加编辑按钮
     30     self.navigationItem.rightBarButtonItem = self.editButtonItem;
     31 
     32 }
     33 #pragma mark - tableView的编辑,删除,添加,移动
     34 //1.判断编辑状态是否打开
     35 -(void)setEditing:(BOOL)editing animated:(BOOL)animated
     36 {
     37     [super setEditing:editing animated:animated];
     38     [self.tableView setEditing:editing animated:animated];
     39 }
     40 //2.确认是否能编辑
     41 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
     42 {
     43     return YES;
     44 }
     45 //3.设置编辑的样式
     46 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
     47 {
     48     if (indexPath.section == 1) {
     49         return UITableViewCellEditingStyleInsert;
     50     }else{
     51         return UITableViewCellEditingStyleDelete;
     52     }
     53 }
     54 //4.提交编辑状态,处理相关的数据,再刷新UI
     55 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     56 {
     57 //    //判断相关的状态
     58     if (editingStyle == UITableViewCellEditingStyleDelete) {
     59              //删除的操作
     60     //①.先操作数据
     61     NSString * key = _keysArray[indexPath.section];
     62     NSMutableArray * array = _allCityDic[key];
     63     [array removeObjectAtIndex:indexPath.row];
     64     //②.再操作UI
     65     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
     66     if (array.count == 0) {
     67         //①.先操作数据
     68         [_keysArray removeObject:key];
     69         [_allCityDic removeObjectForKey:key];
     70         //②.再操作UI
     71         NSIndexSet * indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
     72         [tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];
     73     }
     74     }else if (editingStyle == UITableViewCellEditingStyleInsert){
     75         NSString * key = _keysArray[indexPath.section];
     76         NSMutableArray * array = _allCityDic[key];
     77         //创建数据
     78         CityList *city = [array objectAtIndex:indexPath.row];
     79         //创建数据
     80         [array insertObject:city atIndex:indexPath.row];
     81         //刷新数据方式之一:添加一行
     82         [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
     83     }
     84    
     85 }
     86 #pragma mark - 读取plist数据
     87 -(void)readPlist
     88 {
     89     //1.获取plist文件的路径
     90     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"CityList" ofType:@"plist"];
     91     //2.根据路径获取相应的数据
     92     NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
     93     //3.将字典中有用的数据进行拆解封装在Model类中
     94     //初始化接下来要使用的一个内容
     95     self.allCityDic = [NSMutableDictionary dictionary];
     96     self.keysArray = [NSMutableArray array];
     97     for (NSString *key in dic) {
     98         //此时取出来的数据是城市的相关信息
     99         NSArray *array = dic[key];
    100         //创建一个临时数组
    101         NSMutableArray *cityArr = [NSMutableArray array];
    102         //此时数组遍历出来的数据为字典类型
    103         
    104         for (NSDictionary *dictionary in array) {
    105             //将拆分的数据放在model类中
    106             //创建model类
    107             CityList *cityModel = [[CityList alloc] init];
    108             //使用KVC进行赋值
    109             [cityModel setValuesForKeysWithDictionary:dictionary];
    110             [cityArr addObject:cityModel];
    111         }
    112         //给字典进行赋值
    113         [self.allCityDic setObject:cityArr forKey:key];
    114         //给存放key的一个数组赋值
    115         [self.keysArray addObject:key];
    116     }
    117     
    118 }
    119 #pragma mark - tableView的代理方法
    120 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    121 {
    122     
    123     return self.keysArray.count;
    124 }
    125 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    126 {
    127     //对应城市各个区的个数
    128     NSString *key = self.keysArray[section];
    129     NSArray *array = [self.allCityDic objectForKey:key];
    130     return array.count;
    131 
    132 }
    133 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    134 {
    135     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusableCell"];
    136     if (cell == nil) {
    137         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reusableCell"];
    138     }
    139     NSString *key =self.keysArray[indexPath.section];
    140     NSArray *array =[self.allCityDic objectForKey:key];
    141     CityList *city = array[indexPath.row];
    142     cell.textLabel.text = city.name;
    143     cell.detailTextLabel.text = city.population;
    144     //根据key值获取存储Vlaue的数组
    145     
    146     return cell;
    147 }
    148 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    149 {
    150     NSString *key =self.keysArray[section];
    151    // NSArray *array =[self.allCityDic objectForKey:key];
    152     return key;
    153 }
    154 -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
    155 {
    156 #warning 非常重要
    157     //字典是一种无序的容器,通过字典取key是无序的,但是如果从存储key值得数组中取则是有序的,进行删减增加时,获取的必须是确定的,不能是无序的
    158     return self.keysArray;
    159 }
    160 //当某一行被选中的时候触发的时候可以执行的一些操作
    161 //跳转到tableViewController
    162 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    163 {
    164     FirstTableViewController *firstTVC = [[FirstTableViewController alloc] init];
    165     [self.navigationController pushViewController:firstTVC animated:YES];
    166 }
    167 - (void)didReceiveMemoryWarning {
    168     [super didReceiveMemoryWarning];
    169     // Dispose of any resources that can be recreated.
    170 }
    171 
    172 @end

    上述代码引用了plist文件,

     1 #import "FirstTableViewController.h"
     2 
     3 @interface FirstTableViewController ()
     4 
     5 @end
     6 
     7 @implementation FirstTableViewController
     8 
     9 - (void)viewDidLoad {
    10     [super viewDidLoad];
    11     
    12     
    13     //自身已经添加了tableView
    14 }
    15 
    16 - (void)didReceiveMemoryWarning {
    17     [super didReceiveMemoryWarning];
    18     // Dispose of any resources that can be recreated.
    19 }
    20 
    21 #pragma mark - Table view data source
    22 
    23 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    24 #warning Incomplete implementation, return the number of sections
    25     return 1;
    26 }
    27 
    28 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    29 #warning Incomplete implementation, return the number of rows
    30     return 10;
    31 }
    32 
    33 
    34 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    35     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" ];
    36     if (cell == nil) {
    37         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    38     }
    39     cell.textLabel.text = @"大白";
    40     // Configure the cell...
    41     
    42     return cell;
    43 }
    1 #import <Foundation/Foundation.h>
    2 //设置Model类用于处理数据
    3 @interface CityList : NSObject
    4 @property(nonatomic,strong)NSString *name;
    5 @property(nonatomic,strong)NSString *population;
    6 @end
  • 相关阅读:
    url_encode and url_decode in Shell
    学嵌入式技术,做高端电子工程师
    MCU助推居家移动医疗微型化
    深圳单片机培训,7月盛大开班,报名既送
    中国嵌入式工控机市场前景广阔
    嵌入式 现已发展为 IT行业的主流——高薪,且人才缺乏
    谷歌苹果已“技穷”?移动操作系统2013无创新
    C# 与 C++,语法差别有多小-其他2
    link
    C# 与 C++,语法差别有多小-第三章 C++数据类型 第一部分
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5232671.html
Copyright © 2011-2022 走看看