TableView相关

一. 基础属性

  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容