• UITaleView的基础使用及数据展示操作


    UITableView表视图,是实用的数据展示的基础控件,是继承于UIScrollView,所以也可以滚动。但不同于UIScrollView,UITableView只可以上下滚动,而不能左右滚动。

    因为是数据展示,必然少不了数据的存在,嗯,使用plist文件来获取想要的数据。通过模型来获取。

    说到这样就必须要变到MVC了,MVC是设计模块的核心

    M是指模型,用来获取数据

    V是指视图,用来展示各种数据,各种控件

    C是指视图控制器,能来维持M和V之间的通信

    创建一个类,将相要展示的数据声明成属性

    @interface Student : NSObject
    
    @property (nonatomic, copy)NSString *name;
    @property (nonatomic, copy)NSString *age;
    @property (nonatomic, copy)NSString *number;
    @property (nonatomic, copy)NSString *gender;
    @property (nonatomic, copy)NSString *hobby;

    一定要记得重写处理异常的方法,因为有可能有些数据没有使用到,或者数据拼写错误都不会 产生异常处理。

    - (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
    }

    因为要使用模型,所以 用类似于懒加载的方式,来加载数据

    因为 是在MRC环境下,所以操作完之后要记得释放

    在延展中创建一个保存数据的可变数组dataArrary;

    @implementation ListViewController
    - (void)dealloc{
        [_dataArrary release];
        [_tableView release];
        [super dealloc];
    }
    //加载数据的方法
    - (void)reloadData{
        //获取文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
        //从文件路径中提取数组
        NSArray *arrary = [NSArray arrayWithContentsOfFile:filePath];
        //初始化数组
        _dataArrary = [[NSMutableArray alloc] initWithCapacity:0];
        //遍历数组,进行添加模型
        for (NSDictionary *dic in arrary) {
            Student *student = [[Student alloc] init];
            [student setValuesForKeysWithDictionary:dic];
            [_dataArrary addObject:student];
            [student release];  //一定要释放!
        }
    }

    创建UITableView.

    //执行加载的方法
        [self reloadData];
        
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
        
        //代理
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.view addSubview:_tableView];

    因为要展示数据,所以必须要使用系统内部提供的两个协议,数据源协议UITableDataSource,代理协议UITableDelegate

    因为签订了UITableDataSource协议,所以有两个必须要实现的方法

    //每个分区多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    //每行显示的内容,也就cell的内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    #pragma mark  ------数据源协议的方法--------
    //返回分区多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return  [_dataArrary count];
    }
    //每行显示的内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        //标识符
        static NSString *reuseIdentifier = @"reuse";
        //通过重用标识符找cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        //判断是否为空,是的话重新创建
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]autorelease];
        }
        //选中的一行不会有格式显示,默认选中的为灰色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [[_dataArrary objectAtIndex:indexPath.row] name];
        cell.detailTextLabel.text = [[_dataArrary objectAtIndex:indexPath.row] age];
        return cell;
    }

    因为 要注意重用池的使用,如果 cell每次使用都要重新创建的话,那会造成很大的内存负担,如果使用重用池,当cell滑出屏幕之外,被滑出的cell会放入重用池中,当下次使用,根据重用标识符来判断,如果为空,则重新创建,否则则使用已有的cell。

    每个cell都相当于button的作用,都可以点击,都可以选定,所以系统内部也规定了,cell有专门的点击方法

    //点击单元格触发的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        //push操作,传值
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.student = _dataArrary[indexPath.row];
        [self.navigationController pushViewController:detailVC animated:YES];
        [detailVC release];
    }

    对表视图也可以 进行一些操作,比如删除,添加和移动。

    如果想删除和添加的话有四步可以 实现 这两个功能

    //增加删除功能显示  并不能执行删除操作
    [_tableView setEditing:YES animated:YES];   

    每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用。

    #pragma mark  -------删除,添加数据----------
    
    //1.让将要执行删除,添加操作的表视图处于编辑状态
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
        //先执行父类中的这个方法
        [super setEditing:editing animated:animated];
        //表视图执行此方法
        [self.tableView setEditing:editing animated:animated];
    }
    
    //2.指定表视图中哪些行可以处于编辑状态
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        //前五行可以处于编辑状态
    //    if (indexPath.row < 5) {
    //        return YES;
    //    }
    //    return NO;
        //这个方法默认的是全部行都可以进行编辑
        return YES;
    }
    
    //3.指定编辑样式,到底是删除还是添加
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        //这个方法默认的是删除样式,即UITableViewCellEditingStyleDelete
        
        //添加样式
        //return UITableViewCellEditingStyleInsert;
        
        //前六行是删除样式,之后的全部行是添加样式
    //    if (indexPath.row < 6) {
    //        return UITableViewCellEditingStyleDelete;
    //    }else{
    //        return UITableViewCellEditingStyleInsert;
    //    }
    }
    //4.不管是删除还是添加,这个方法才是操作的核心方法,当点击删除或者添加按钮时,需要做什么事情,怎么样才能完成删除还是添加操作,全部在这个方法内部指定。
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        //表视图开始更新
        [tableView beginUpdates];
        
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //将该位置下的单元格删除
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            //删除数据数组中,与该单元格绑定的数据‘
            [_dataArrary removeObjectAtIndex:indexPath.row];
        }else if (editingStyle == UITableViewCellEditingStyleInsert){
            Student *student = _dataArrary[indexPath.row];        
            //构建一个位置信息
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
            [_dataArrary insertObject:student atIndex:index.row];
        }
        //表视图结束更新
        [tableView endUpdates];
    }

    同样的 ,移动也是相同的步骤,不同于删除和添加的方法就是核心处理方式的不同,也就是第四步

    #pragma mark  ------表视图移动的操作---------
    //1.移动的第一步也是需要将表视图的编辑状态打开
    //2.指定哪些行可以进行移动
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
        //默认都可以移动
        return YES;
    }
    
    //3.移动完成之后要做什么事,怎么完成移动
    //sourceIndexPath原位置
    //destinationIndexPath新位置
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
        //先记录原有位置下的模型数据
        Student *student = _dataArrary[sourceIndexPath.row];
        [student retain];
        //删除原位置下的模型数据
        [_dataArrary removeObjectAtIndex:sourceIndexPath.row];
        //在新位置将记录的模型数据添加到数据数组中
        [_dataArrary insertObject:student atIndex:destinationIndexPath.row];
        [student release];
    }

    移动呢,就是先记录好原有位置的数据,再删除原位置的数据,然后在新位置上,把记录的数据添加到模型数组中。

    注意:

    UITableView,是展示数据的基础,对于新手来说,还是很容易上手的,数据源协议有除了两个必要的方法之外,还可以通过方法来显示有多少个分区,并显示分区标题,根据分区显示索引。

  • 相关阅读:
    LeetCode 225. 用队列实现栈 做题笔记
    杨辉三角
    字母图形
    01字符串
    圆的面积
    饮料和啤酒
    进制转换
    从今天起 复习算法
    乘法群
    Paillier同态加密的介绍以及c++实现
  • 原文地址:https://www.cnblogs.com/xueyao/p/5186214.html
Copyright © 2020-2023  润新知