美文网首页
TableView相关

TableView相关

作者: erbai | 来源:发表于2015-12-30 09:04 被阅读579次

一. 基础属性

  1. tableView style 只能在初始化中设置

      @property(nonatomic, readonly) UITableViewStyle style 
    
      typedef NS_ENUM(NSInteger, UITableViewStyle) {
      UITableViewStylePlain,          // regular table view
      UITableViewStyleGrouped         // preferences style table view
    };
    
  2. 设置分割线style(枚举):

     @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
    
     typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
         UITableViewCellSeparatorStyleNone, // 不显示分割线
         UITableViewCellSeparatorStyleSingleLine, // 一条风分割线
         UITableViewCellSeparatorStyleSingleLineEtched // 与None 看起来相同 = = 
     } __TVOS_PROHIBITED;
    
  3. 设置分割线颜色

     @property(nonatomic, retain) UIColor *separatorColor
    
  4. 设置分割线效果(IOS8之后可用) 具体实现效果未发现,有待考证

    @property(nonatomic, copy) UIVisualEffect *separatorEffect
    ableView.separatorEffect = [UIVibrancyEffect effectForBlurEffect:
    [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    //枚举
    typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
        UIBlurEffectStyleExtraLight,
        UIBlurEffectStyleLight,
        UIBlurEffectStyleDark
    } NS_ENUM_AVAILABLE_IOS(8_0);
    
  5. tableView backgroundView 与 backgroundColor
    区别 :backgroundView 是处于TableView父视图与cell层之间的一层View,在滑动时显示出来的就是backgroundView,而backgroundColor是整个tableView的颜色
    而backgroundColor会影响分割线的颜色,backgroundView只会在tableView滑到极限的时候能看到。另外,backgroundView只有设置一个View的时候,设置颜色才会生效(笔者尝试添加一个 ImageView 显示图片,并不能显示)。

二.进阶属性与方法

  1. 头视图与尾视图 (Accessing Header and Footer Views)
  • tableView头尾视图(注:不同于Session的头尾视图)
    需要实例化一个UIView对象,头尾视图指针指向这个UIView对象,与Session的重用机制不同,tableView的头尾不需要考虑重用池,一般建议轮播图写在此处
    @property(nonatomic, retain) UIView *tableHeaderView
    @property(nonatomic, retain) UIView *tableFooterView
  1. Session 头尾高度
    虽然此方法可以设置分区的头尾高度,但是不建议使用,建议使用代理方法设置(详见下文)。使用此方法有可能第一个分区的高度还是系统默认高度
    @property(nonatomic) CGFloat sectionHeaderHeight
    @property(nonatomic) CGFloat sectionFooterHeight

  2. Session 头尾注册与使用
    // 注册
    - (void)registerClass:(Class nullable)aClass forHeaderFooterViewReuseIdentifier:
    (NSString * nonnull)identifier
    // 当使用xib创建视图的时候 使用此方法 需要初始化UINib∂
    - (void)registerNib:(UINib * nullable)nib forHeaderFooterViewReuseIdentifier:
    (NSString * nonnull)identifier
    // 从重用池调用
    - (_kindofUITableViewHeaderFooterView * nullable)
    dequeueReusableHeaderFooterViewWithIdentifier:(NSString * nonnull)identifier

  3. 获取指定Session头尾视图
    // section 为指定分区
    - (UITableViewHeaderFooterView * nullable)headerViewForSection:(NSInteger)section

     - (UITableViewHeaderFooterView * nullable)footerViewForSection:(NSInteger)section
    
  4. 返回cell位置相关(Accessing Cells and Sections)

    • 返回指定位置NSIndexPath

      // 给定cell 返回NSIndexPath
       - (NSIndexPath * nullable)indexPathForCell:(UITableViewCell * nonnull)cell
      // 返回给定坐标处的cell,此坐标是相对于整个tableView,而不是当前屏幕
       - (NSIndexPath * nullable)indexPathForRowAtPoint:(CGPoint)point 
      // 与上面一个类似,给一个范围,返回指定范围内所有cell的NSIndexPath 数组
      // (当tableView类型为Plain时 因为有分区,所以数组内将包括下一分区聂内容)
       - (NSArray<NSIndexPath *> * nullable)indexPathsForRowsInRect:(CGRect)rect
      // 返回当前屏幕所有cell 的NSIndexPath
      @property(nonatomic, readonly) NSArray<NSIndexPath *> indexPathsForVisibleRows
      
  5. 返回cell本身

      // 根据NSIndexPath 返回对应cell
      - (UITableViewCell * nullable)cellForRowAtIndexPath:(NSIndexPath * nonnull)indexPath
      // 返回当前屏幕所有cell
      @property(nonatomic, readonly) NSArray<__kindof UITableViewCell *> visibleCells
    
  • 滚动到指定位置(Scrolling the Table View)
    跳转到指定的Index

      - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:
      (UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
      //  其中 scrollPosition 为枚举值 
      typedef enum {
         UITableViewScrollPositionNone,
         UITableViewScrollPositionTop,
         UITableViewScrollPositionMiddle,
         UITableViewScrollPositionBottom 
      } UITableViewScrollPosition;
      // 其中indexPath 为tableViewCell 的位置 可自己定义
      NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    
  1. 模糊计算cell高度
    此方法在不同iOS版本上功效不一样,模糊计算效果很差。cell自适应高度一直是很让人困扰的地方,此处贴出CSDN中比较好的解决方法(具体内容待笔者研究后在分享) http://www.csdn.net/article/2015-05-19/2824709-cell-height-calculation

     @property(nonatomic) CGFloat estimatedRowHeight
    
  2. 选择cell的相关设置(Inserting, Deleting, and Moving Rows and Sections)

  • 是否允许选择Cell (编辑状态的设置下文会提到)
    @property(nonatomic) BOOL allowsSelection // 非编辑状态
    @property(nonatomic) BOOL allowsSelectionDuringEditing// 编辑状态
  1. 是否允许选择多个行进行编辑
    @property(nonatomic) BOOL allowsMultipleSelection // 非编辑状态是否允许被选中
    @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing //编辑状态是否允许被选
    默认为NO, 当设置为YES的时候,若没有去除点击的灰色效果会发现可以有多个Cell变成点击的样子,再次点击取消,此过程会调用回调方法。反之,只能有一个cell为点击状态
    配合回调方法使用()
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

  2. 返回当前选中cell的NSIndexPath

      // 返回当前选中的cell的IndexPath
      - (NSIndexPath *)indexPathForSelectedRow
      // 返回当前选中的cell的IndexPath,此方法返回值为一个数组 。
      // 使用在allowsMultipleSelection = YES 的时候
      - (NSArray *)indexPathsForSelectedRows
    
  3. 设置cell选中和取消选中状态
    - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated: (BOOL)animated
    scrollPosition:(UITableViewScrollPosition)scrollPosition
    // UITableViewScrollPosition Cell选中后的位置
    - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath
    animated:(BOOL)animated

  • Cell的编辑状态设置(Managing the Editing of Table Cells)
    //当要对cell编辑的时候,要将此属性设为YES,此属性不可以直接赋值,需要调用下面的set方法
    @property(nonatomic, getter=isEditing) BOOL editing

    - (void)setEditing:(BOOL)editing animated:(BOOL)animate
    
  • 编辑(Inserting, Deleting, and Moving Rows and Sections)

    // 当tableview需要同时执行多个动画时,才会用到beginUpdates函数,它的本质就是建立了CATransaction这个事务。
    // 如果你仅仅更改了UITableView的cell的样式,那么应该试试能否通过调用beginUpdates 和 reloadRowsAtIndexPaths 
    // 来实现效果,而不是调用tableview的reloadData方法去重新加载全部的cell
    - (void)beginUpdates
    - (void)endUpdates
    // CELL的增加、删除和移动
    - (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths 
            withRowAnimation:(UITableViewRowAnimation)animation
    - (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths 
            withRowAnimation:(UITableViewRowAnimation)animation
    - (void)moveRowAtIndexPath:(NSIndexPath * nonnull)indexPath*toIndexPath:(NSIndexPath * nonnull)newIndexPath  
    // 分区头的增加、删除和移动
    - (void)insertSections:(NSIndexSet * nonnull)sections*withRowAnimation:(UITableViewRowAnimation)animation
    - (void)deleteSections:(NSIndexSet * nonnull)sections*withRowAnimation:(UITableViewRowAnimation)animation
    - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
    
  • 刷新TableView (Reloading the Table View)
    // 刷新 整个tableView
    - (void)reloadData
    // 刷新某一个Cell
    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths
    withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新某一个分区
    - (void)reloadSections:(NSIndexSet * nonnull)sections withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新分区的标题
    - (void)reloadSectionIndexTitles

  • 索引相关(Configuring the Table Index)
    // 当设置的数值大于cell的个数则不显示索引
    @property(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount
    // 索引颜色
    @property(nonatomic, retain) UIColor *sectionIndexColor
    // 索引的背景色
    @property(nonatomic, retain) UIColor *sectionIndexBackgroundColor
    // 点击索引时的颜色
    @property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor

相关文章

网友评论

      本文标题:TableView相关

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