• iOS学习之UItableView


    一些相关的总结,有点乱.

    •  UITableViewiOS中提供的用来以列的形式展示数据的视图,叫做表现图,但是只有一列,而且只能在垂直方向滚动.继承自UIScrollView.
    •  UITableView由多个分区组成(相当于班级的分组),每个分区由多行组成(相当于每个分组下的人).
    •  UITableView有两种样式,PlainGroup样式,一旦设置之后,后期不能更改. 

     


    继承自UITableViewController 继承自UIViewController的区别. (UITableViewControllerUIViewController的子类)

    • .前者根视图是tableView, 而后者根视图是UIView. 前者不需要指定dataSource,delegate.服从协议. 而后者需要.
    • 前者不需要重写setEditing:animated:方法控制tableView进入编辑状态,而后者需要自己实现.
    • 前者对于UITableViewDataSource协议中的常用方法已经自动生成,而后者需要自己添加对应的方法.

    何时需要继承自UITableViewController?

        当前页面信息的展示主要是以列的形式来展示的场景下, 都可以直接继承自UITableViewController.

        在继承自UITableViewController的视图控制器中访问tableView.

        1.self.view  根视图就是tableView.

        2.self.tableView 有对应的tableView属性.


    UITableView协议中的一些方法

    UITableViewDataSource协议

      1.配置TableView一共有几个分组

      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

      2.配置tableView每个分区对应的行数

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

        3.配置用来显示每一行数据的cell.(UITableViewCell)

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //1.创建重用标识符.
        static NSString *identifier = @"heihei";
        //2.根据重用标识符去重用队列中取可重用的cell.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        //3.判断是否成功取到可重用的cell.cell是否为空.
        if (!cell) {
            //4.cell为空,说明没有成功取到cell.则创建一个cell.
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //辅助视图样式,小箭头
        }
        NSDictionary *dic = self.addressDic[self.sortedKeys[indexPath.section]][indexPath.row];
        cell.textLabel.text = dic[@"name"];
        cell.detailTextLabel.text = dic[@"phone"];
        cell.imageView.image = [[UIImage imageNamed:dic[@"imageName"]] scaleToSize:CGSizeMake(40, 40)];
        return cell;
    }

        4.配置每个分区的页眉 

        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

        5.配置tableView右侧的分区索引

        - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

        //编辑相关的协议方法

        6.设置tableView的哪些行可以允许编辑

        - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

        7.提交编辑操作时触发(默认的时删除操作)

        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

    //提交编辑操作, 对删除操作作出处理.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        //总共分两步:1.修改数据源 2.修改界面
        //1.获取删除行对应的分区key.(是B分组,还是C分组)
        NSString *key = self.sortedKeys[indexPath.section];
        //2.根据key获取对应的可变数组.
        NSMutableArray *group = self.addressDic[key];
        
        if (editingStyle == UITableViewCellEditingStyleInsert) {
            //处理插入操作
            //1.修改数据源
            NSDictionary *dic = @{@"name":@"Frank", @"age":@"18", @"gender":@"man", @"phone":@"110", @"imageName":@""};
            [group insertObject:dic atIndex:indexPath.row];
            //2.修改界面
            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        } else {
            //处理删除操作
            //需要判断是否要删除一个分区.
            if (group.count == 1) {
                //删除分区
                //1.修改数据源
                //从字典中根据key移除对应的元素.
                [self.addressDic removeObjectForKey:key];
                //从排好序的key值数组中移除对应的key.
                [self.sortedKeys removeObject:key];
                //2.修改界面
                NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
                [tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];
            } else {
                [group removeObjectAtIndex:indexPath.row]; //删除行对应的字典.
                //删除界面上的一行.
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
            }
        }
    }

        //移动相关的协议方法

        8.设置tableView哪些行可以允许移动

        - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

        9.提交移动操作触发.

        - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;

    //提交移动操作.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        //因为移动操作界面已经发生变化,我们只需要修改数据源即可.
        //1.获取到分区对应的数组.
        NSMutableArray *group = self.addressDic[self.sortedKeys[sourceIndexPath.section]];//分区对应的数组
        //2.将原位置对应的元素取出来保存.
        NSDictionary *dic = [group[sourceIndexPath.row] retain]; //retain 引用计数加1, 否则移除时就造成引用计数为0,空间回收了.
        //3.将原位置对应的元素删除掉.
        [group removeObjectAtIndex:sourceIndexPath.row];
        //4.将保存的元素插入到目的位置.
        [group insertObject:dic atIndex:destinationIndexPath.row];
        //5.释放所有权
        [dic release];
    }

     

      UITableViewDelegate协议

        1.当tableView的行被选中时触发

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

        2.tableView的行被取消选中时触发

     

        - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

        3.配置tableView某一行的高度

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

        //编辑相关

        4.设置tableView的编辑样式

        - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

        5.设置删除时确认按钮的标题.

        - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;

        //移动相关

        6.设置tableView限制跨区移动

        - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;

    - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
        //sourceIndexPath 移动之前的位置
        //proposedDestinationIndexPath 即将要移动到的位置
        if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
            return proposedDestinationIndexPath;
        }
        return sourceIndexPath;
        
    }

     


     

    UITableView编辑步骤:

        1.在导航条上添加Edit按钮. 重写setEditing:Animated:方法.

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

        2.控制tableView的可编辑状态.

        3.设置tableView的哪些行可以允许编辑. (dataSource)

        4.设置编辑样式. (delegate)

        5.提交编辑操作. (dataSource) (1)修改数据源 (2)修改界面


     

  • 相关阅读:
    toad 快捷键大全
    validateRequest 相关的作用
    为何有着良好设计的系统代码反而不容易看懂?
    致命错误: zlib.h:没有那个文件或目录
    全局变量相互依赖和初始化顺序的解决办法
    解决“possibly undefined macro: AC_PROG_LIBTOOL”
    Rsync完全配置
    undefined reference to `clock_gettime'
    解决“configure: line 2747: g: command not found”
    openssl编程轻松入门(含完整示例)
  • 原文地址:https://www.cnblogs.com/ErosLii/p/4498881.html
Copyright © 2020-2023  润新知