UI第九、第十、第十一课UITableView

主要内容:UITableView

一、表视图

       UITableView表示图:通常用来管理一组具有相同数据结构的数据

       UITableView继承与UIScrollView,所有可以滚动,表示图的每一个数据都显示在UITableViewCell对象中,表示图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始

二、表示图的创建

       

       每一行中要显示的数据,需要有一个数据源DataSource

        那么需要遵守两个协议:

               - (NSInteger)tableView:(UITableVIew *)tableView  numberOfRowsInSection:(NSInteger)section;每个分区有多少行

               - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowsAtIndexPath:(NSIndexPath *)indexPath;每行显示的内容

         但是我们写的时候都会在前面先实现这个方法

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

         UITableView中每一个单元格被称为一个cell(UITableViewCell),系统预置了4中样式的Cell(枚举类型)



          重用机制:

                靠mutableSet实现重用机制

                出屏幕的Cell会呗添加到mutableSet中,进入屏幕的Cell,先从set中获取,如果获取不到,才创建一个cell,在cell显示之前,给cell赋上相应的内容,cell的reuseIdentifier是重用的关键

三、UITableView编辑

       cell的添加、删除

       编辑的步骤:

              1、让tableView处于编辑状态

                    - (void)setEditing:(BOOL)editing  animated:(BOOL)animated;

              2、指定tableview哪些行可以编辑

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

              3、指定tableView的编辑样式

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

              4、编辑完成(先操作数据源、在修改UI)

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

      移动的步骤:

               1、让tableview处于编辑状态

                      - (void)setEditing:(BOOL)editing  animated:(BOOL)animated;

               2、指定哪些行可以移动

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

               3、移动完成

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

        移动过程中,检测移动过程,限制跨区移动

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




UITableView的方法

设置有多少个分区

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


设置每个分区有多少行

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


设置每行显示的内容

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


设置分区头标题

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


设置右侧索引

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


设置头标题的高度

- (CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section;


设置尾标题的高度

- (CGFloat)tableView:(UITableView *)tableView  heightForFooterInSection:(NSInteger)section;


设置头标题内显示的View

- (UIView *)tableView:(UITableView *)tableView  viewForHeaderInSection:(NSInteger)section;


设置尾标题内显示的View

- (UIView *)tableView:(UITableView *)tableView  viewForFooterInSection:(NSInteger)section;


设置每行的高度及选中

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值