美文网首页
iOS_UI_10_UITableView

iOS_UI_10_UITableView

作者: 孤城99 | 来源:发表于2016-12-09 21:47 被阅读0次

    第十章 UITableView

    一、UITableView的概念
    1.UITableView继承于UIScrollView,可以滚动
    2.UITableView的每一条数据对应的单元格叫做cell,是UITableViewCell的一个对象,继承于UIView。
    3.UITableView可以分区显示,每个分区称为section,每一行称为row,编号都从0开始,代表一个UITableViewCell在UITableView上的位置
    4.系统提供了一个专门的类来整合section和row,叫做NSIndexPath
    
    二、UITableView的基本使用
    1.创建:一种是在ViewController中alloc和initWithFrame(self.view.bounds),一种是创建一个类继承于UITableViewController(不需要创建对象直接使用self.tableBar调用,不需要指定代理,只实现协议方法即可)
    2.UITableView的初始化方法包含一个UITableViewStyle类型的参数(枚举类型)
          UITableViewStylePlain---默认
          UITableViewStyleGrouped
    3.UITableView显示的相关属性
          rowHeight(行高)
          separatorStyle(分割线样式)
          separatorColor(分割线颜色)
          tableHeaderView(UITableView的置顶视图)
          tableFooterView(UITableView的置底视图)
    
    三、UITableView显示数据
    1.UITableView的两个重要属性(协议代理相关)
          dataSource(显示数据相关的代理)
          delegate(视图操作相关的代理)
    2.协议中两个必须实现的协议方法
        UITableView每个分区包含的行数
             - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
        每一行要显示的cell
             - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
    3.UITableVIewCell默认的3个视图属性
             UIImageView* imageView----》图片视图
             UILabel* textLabel----》标题视图
             UILabel* detailTextLabel----》副标题视图
             UITableViewCellAccessoryType* accessoryType---->设置右侧辅助视图的样式
                UITableViewCellAccessoryNone----》没有辅助视图
                UITableViewCellAccessoryDisclosureIndicator----》箭头
                UITableViewCellAccessoryDetailDisclosureButton----》详情按钮加箭头-
                UITableViewCellAccessoryCheckmark---->对号
                UITableViewCellAccessoryDetailButton----》详情按钮
              accessoryView----》当系统提供的辅助样式不能满足你,你就可以自定义视图
    
    四、UITableViewcell的重用机制
    1.重用机制的流程
        1.当一个cell被滑出屏幕,这个cell会被系统放到相应的重用池中
        2.当tableView需要显示一个cell,就会先去重用池中尝试获取一个cell。
        3.如果重用池没有cell,就会创建一个cell
        4.取得cell之后会重新赋值进行使用
    2.重用cell的代码流程
        1.注册cell类----iOS 6.0 以后
            - (void)registerClass:(Class)cellClass forCellReuseldentifier:(NSString*)identifier;
        2.获取重用池中的cell的方法
            - (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier---->需要提供一个重用标识
        3.应用:
            注册:[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
            获取:UITableViewCell* cell = [tableView dequeuReuseableCellWithIdentifier:@"CELL"];
    
    五、UITableView的常用协议方法
    UITableViewDataSource
        1.UITableView分区个数
            - (NSInteger)numberOfSectionInTableView:(UITableView*)tableView;
        2.分区的顶部标题
            - (NSString*)tableView:(UITableView*)tableView titleHeaderInSection:(NSInteger)section;
        3.分区的底部标题
            - (NSString*)tableView:(UITableView*)tablrView titleFooterInSection:(NSInteger)section;
        4.UITableVie右侧的索引目录
            - (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;
    UITableViewDelegate
        1.告诉delegate选中了一个cell
            - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
        2.每一行的高度
            - (CGFloat)tableView:(UITableView*)tableView heightForRowAtInxPath:(NSIndexPath*)indexPath;
        3.每一个分区的顶部高度
            - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSIntger)section;
        4.每一个分区的顶部自定义视图
            - (UIView*)tableView:(UITableView*)tableView viewForJeaderInSection:(NSIntger)section;
        5.cell的触摸事件,参数:indexPath:被点击cell所在的位置(那个分区的哪一行)
            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    
    注意:
    1.tableView所呈现的内容都是来自于cell,我们想要将呈现在cell上,相当于我们想将M(数据)显示在V(tableView)上面,我们就需要C来做桥梁;这里面就涉及到了C和V之间的通信。一般C和V之间的通信,系统习惯使用协议代理模式
    2.自己创建单元格的时候可以设置单元格样式(initWithStyle)
         样式:UITableViewCellStyleDefault----》一个imageView和一个label
               UITableViewCellStyleValue1
               UITableViewCellStyleValue2----》系统不提供imageView,textLabel.text:的字体颜色默认为蓝色
               UITableViewCellStyleSubTitle

    相关文章

      网友评论

          本文标题:iOS_UI_10_UITableView

          本文链接:https://www.haomeiwen.com/subject/efjymttx.html