UITableView(一)---- 基础

来源:互联网 发布:手机学全站仪的软件 编辑:程序博客网 时间:2024/06/06 02:11

基本概念

  • UITableView实例表示按照等级列表的方式显示和编辑信息。
  • UITableView是UIScrollView的子类,允许用户滑动显示table,但是UITableView只允许垂直滑动。
  • CELL:Table是有Cell(UITableViewCell对象)组成,UITableView使用这些对象绘制可见的行。
    • Cells拥有自己的内容:title和image,并且靠近右边缘能够拥有辅助视图。
    • 标准的辅助视图一般为指示器或者详情指示按钮:前者指向数据层次的下一个级别;后者指向选择item的详细视图
    • 辅助视图可以为框架控制,例如:switched, sliders, custom views。
  • Table视图可以插入、删除以及重新排序
  • Section:Table View由0或多个区域组成,每一个有自己的行。
    • Section由table view内的索引标识
    • 行由section内的索引标识
    • 即:table对应到每一行是一个二级索引
  • Table view的两种样式UITableViewStylePlainUITableViewStyleGrouped
    • 创建table时必须制定一个样式,并且指定之后无法再修改。
    • tableView的group样式为table和cell提供了默认的背景颜色和背景视图
  • UITableView基于NSIndexPath申明了一个种类,代表了行列的索引。
    • 通过indexPathForRow:inSection方法可以构造给定行列的index path
    • 如果一个tableView有多个section,则需要采用二级索引来制定一个cell:section id->index path
  • Data & Delegate: UITableView必须有一个数据对象和一个代理对象(delegate)
    • delegate通常为应用代理或者自定义的UITableViewController对象,代理必须与UITableViewDelegate协议一致。Delegate管理table行的配置和选择,行的重排序、强调、辅助视图以及编辑操作等。
    • 数据对象必须与UITableViewDataSource协议一致。数据对象为table的构建,以及当掺入、删除、重排序table的行时管理数据mode提供了信息基础。
  • 当发送setEditing:animated:消息时,table view会进入编辑模式,此时会显示编辑或者重排序可见的行,这些操作基于UITableViewCell相关的editingStyle

序列

  • tableView的插入或删除控制会导致数据源收到一个tableView:commitEditingStyle:forRowAtIndexPath的消息,可以通过调用deleteRowsAtIndexPaths:withRowAnimation:或者inserRowsAtIndexPaths:withRowAnimation:提交删除或插入操作。
  • 当将table-view cell的属性showsReorderControl属性设置为YES时,数据源会收到tableView:moveRowAtIndexPath:toIndexPath:消息
  • 通过实现tableView:canMoveRowAtIndexPath:方法选择性的为cell提供移除重排序控制
  • UITableView重写了layoutSubviews方法,只有当创建UITableView的新实例或者赋值一个新的数据源对象时,才会调用reloadData

状态保护

  • 在iOS6及之后的版本中,如果对table view的restorationIdentifier属性赋值时,它会视图保护当前选择的行和第一个可见行。
  • table view的数据源可能会适应UIDataSourceModelAssociation协议,该协议提供了一种与行在table view 的位置无关,但能够确定行内容的方法。
    • 如果table的数据源适应了该协议,则当改变第一个可见行或者任何选择的单元时会咨询数据源。在恢复的过程中,将identifiers转换成index path会咨询数据源,并重建第一个可见行之间的链接,并重新选择cell。
    • 如果没有没有适应该协议,则滑动位置会直接被保存和恢复,选择单元的index path也是如此

Tasks 


初始化对象

  • -(id)initWithFrame:(CGRect) frame style:(UITableViewStyle)style
    • 创建一个tableView,创建时必须制定一个style,该style不能够再修改,默认为:UITableViewStylePlain
    • UITableViewStylePlain:header和footer作为内联分隔器显示,当table view滚动时浮动。
    • UITableViewStyleGrouped:table view的sections表现为不同的行的组。header和footer部分不浮动

配置table view

  • @proper(nonatomic, readonly) UITableViewStyle style
    • table view的样式
  • -(NSInteger)numberOfRowsInSection:(NSInteger)section
    • 返回指定区域的行数
  • -(NSInteger)numberOfSections
    • 返回receiver的section数
  • @proper(nonatomic) CGFloat rowHeight
    • receiver中每一行的高度
  • @proper(nonatomic, readwrite, retain) UIView *  backgroudView
    • table view的背景视图。table view的背景视图自动适应table view的大小。该视图放在所有cells、header views和footer views之后。当需要设置背景颜色时,该属性必须设置为nil
  • @proper(nonatomic)  UIEdgeInsets separatorInset
    • 执行单元分隔的默认inset。在iOS7和之后的版本中,单元分隔器并不表示table view的所有边的方式。该属性设置了table中所有cell的默认inset,类似于rowHeight设置cell的默认高度
  • @proper(nonatomic)  UITableViewCellSeparatorStyle separatorStyle
    • table单元分隔符的style
    • Selection

      Method

          Argument

      None

      separatorStyle

          UITableViewCellSeparatorStyleNone

      Single Line

      separatorStyle

          UITableViewCellSeparatorStyleSingleLine

      Single Line Etched

      separatorStyle

          UITableViewCellSeparatorStyleSingleLineEtched


创建table view的cells

  • -(void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
    • 通过一个指定的标识符,在table view中注册一个包含cel的nib对象。其中,两个参数均不能为nil(identifier不能为空字符串),其中identifier表示cell的可重用标识符。
  • -(void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
    • 注册一个用于创建table cell的类。在从队列中获取任何cell之前,会调用这个方法或者registerNib:forCellReuseIdentifier方法告诉table如何创建新的cell。如果指定类型的cell当前不再reuse queue中,table view会使用提供的信息自动创建cell对象。
  • -(id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndextPath:(NSIndexPath *)indexPath
    • 通过指定的重用标识符返回一个可重用的table-view cell对象。
    • 其中,indexPath指定cell的位置,当询问cell,并且只是转嫁成本时数据源收到该信息。该方法使用index path来执行基于cell位置的额外配置。
    • 由于性能原因,当将cell赋值到行时(调用方法tableView:cellForRowAtIndexPath:),table view的数据源应该自动重用UITableViewCell对象。
    • table view维护了一个数据源标识重用的queue或者UITableView cell的链表。当要为table view提供一个新的cell时会从数据源调用该方法。该方法在有一个可用单元或者基于之前注册提供的nib file或者class创建cell时从队列中获取一个cell。
  • -(id)dequeueReusableCellWithIdentifier:(NSString *)identifier
    • 通过identifier返回一个可重用的table-view cell对象

访问header和footer视图

  • -(void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
    • 通过制定的identifier注册一个用于创建table的header或者footer的类。在从队列获取任何header或者footer视图之前,调用这个方法或者registerNib:forHeaderFooterViewReuseIdentifier:方法来告诉table view如何创建一个实例。如果制定类型的视图当前不再重用队列中,table view会采用提供的信息自动创建。
  • -(void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier
    • 通过制定的identifier注册一个包含cell的nib对象。
    • 调用情况与registerClass:forHeaderFooterViewReuseIdentifier类似
    • 如果之前使用了相同identifier注册了一个class或者nib,该制定的nib将替换旧的nib。当需要unregister时,仅需要传入nil即可。
  • -(id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
    • 通过制定的标识符返回一个可重用的header或者footer。
    • 与cell类似,由于性能原因,当需要提供header或者footer时table view的delegate会自动重用UITableViewHeaderFooterView对象。
    • 一个table view中维护了标识table view的delegate中可重用的队列或者UITableViewHeaderFooterView对象的链表。
    • 通过在创建对象时赋一个可重用的标识符来标识可重用。
    • 通过该方法可以方位之前创建的指定模板的footer或者header视图,通过reuseIdentifier属性能够访问视图的可重用标识符。
  • @proper(nonatomic, retain) UIView * tableHeaderView
    • 返回table下面显示的辅助视图
  • @proper(nonatomic, retain) UIView *  tableFooterView
    • 返回table上面显示的辅助视图。
  • @proper(nonatomic) CGFloat sectionHeaderHeight
    • footer section的高度。该非负值仅在delegate没有实现tableView:heightForFooterInSection:方法时有效。
  • @proper(nonatomic) CGFloat sectionFooterHeight
    • header section的高度。该非负值仅在delegate未实现tableView:heightForHeaderInSection:方法时才使用。
  • -(UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
    • 返回指定section的footer view。table在plain style时,section的index为0。
  • -(UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section
    • 返回指定section的header view。

访问Cell和Section

  • -(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
    • 返回指定index path的table cell
  • -(NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
    • 给定table-view的cell返回一个代表行和section的index path。
  • -(NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
    • 根据给定的pointl返回一个代表行和section的index path。
  • -(NSArray *)indexPathForRowsAtRect:(CGRect)rect
    • 给定一个rectangle,返回在rect范围内的所有行的index paths
  • -(NSArray *)visibleCells
    • 返回receiver的课件cells
  • -(NSArray *)indexPathsForVisibleRows
    • 返回receiver中课件行的index paths

预算元素高度

  • @proper(nonatomic) CGFloat estimatedRowHeight
    • 预估table view中行的高度。
    • 提供一个非负的预估值能够有效的提高加载table view的性能。如果table中包含不同高度的行,可能会在加载时消耗计算高度的时间。因此,使用预估值能够减低加载和滑动时几何计算的代价。
  • @proper(nonatomic) CGFloat estimateSectionHeaderHeight
    • 预估table view中header的高度。对性能的影响与estimatedRowHeight类似。
  • @proper(nonatomic) CGFloat estimateSectionFooterHeight
    • 预估table view中footer得高度。对性能的影响与estimatedRowHeight类似。

滚动Table View

  • -(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
    • 滑动table view直到指定的inexPath在screen的特殊位置上。
    • 调用该函数不会触发delegate接收scrollViewDidScroll:消息,该方法时程序触发用户界面的操作。
  • -(void)scrollToNearestSelectedRowAtPosition:(UITableViewScrollPosition)scrollPositionanimated:(BOOL)animated
    • 滑动table view直到选择的行最接近于由scrollPosition指定的位置。

管理选择

  • -(NSIndexPath *)indexPathForSelectedRow
    • 返回选择行的index path。当同事选择了多行时,返回选择数组中得第一个行的index-path对象,该对象有最小的section和row值。
  • -(NSArray *)indexPathsForSelectedRows
    • 返回选择rows的index path数组对象。
  • -(void)selectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
    • 通过index path选择一个行,选择性的滑动到receiver中行的位置。
  • -(void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
    • 取消给定index path行的选择。
    • 调用该函数不会导致delegae接收到tableView:willSelectRowAtIndexPath:或者tableView:didSelectRowAtIndexPath:方法,或者向observer发送UITableViewSelectionDidChangeotification消息
  • @proper(nonatomic) BOOL allowsSelection
    • 是否允许用户选择一行。如果设置为YES:用户可以选择行;设置为NO,不允许。该属性仅当table view处于非编辑模式下时才影响用户的选择。
  • @proper(nonatomic) BOOL allowsMultipleSelection
    • 是否允许用户在非编辑模式下选择多行。当设置为YES时:单击的行会出现一个check标识,再次单击会移除该标识。如果调用indexPathsForSelectedRows可以获得指定选择行的index
  • @proper(nonatomic) BOOL allowsSelectionDuringEditing
    • 是否允许在编辑模式下选择一行。默认为NO,若设置为YES:用户可以在编辑模式下选择多行。
    • 如果希望忽略模式限制行的选择,可以使用allowsSelection属性。
  • @proper(nonatomic) BOOL allowsMultipleSelectionDuringEditing
    • 是否允许在编辑模式下选择多行。默认为NO,若设置为YES:check标识会出现在选择行旁边。
    • 另外,UITableView在进入编辑模式时不查询编辑类型。

插入、删除以及移动行和section

  • -(void)beginUpdates
    • receiver调用insert, delete, select row或section方法的开始。
    • 如果希望在插入、删除和选择操作同时进行动画,则调用该方法。该系列的方法必须以endUpdates结束。
    • 如果不想再begin-end之间进行插入、删除和选择操作,类似于行计数的table属性可能会非法。不能在begin-end之间reloadData,如果想在这之间调用该函数,则需要自己执行animation操作。
  • -(void)endUpdates
    • 结束receiver的insert, delete, select row或section方法调用。
  • -(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    • 向recevier的指定位置插入一行。
    • 常用动画:
      • typedef enum {

           UITableViewRowAnimationFade,

           UITableViewRowAnimationRight,

           UITableViewRowAnimationLeft,

           UITableViewRowAnimationTop,

           UITableViewRowAnimationBottom,

           UITableViewRowAnimationNone,

           UITableViewRowAnimationMiddle,

           UITableViewRowAnimationAutomatic = 100

        } UITableViewRowAnimation;

    • UITableView会立即调用相关delegate和data source的方法来获取cells和可见cell的其他部分。
    • 当该函数在由beginUpdates和endUpdates定义的动画块之间调用时,UITableView在处理完行or section的删除操作之后再执行插入,此种处理方式不管插入和删除操作的顺序。与向可变数组的插入、删除操作不同,成功的插入和删除操作会直接影响数组的index。
  • -(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    • 从receiver的指定位置删除一行。
    • 其他说明类似于insertRowsAtIndexPaths:withRowAnimation:
  • -(void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
    • 将行从指定位置移动到新位置。
    • 可以将该操作与行移动操作和插入删除操作组合进beginUpdates-endUpdates块中,让若有的操作在一个动画中发生。
    • 与插入、删除操作不同,该方法没有动画参数。当行移动时,移动行的动画直接从起始位置到结束为止。另外,不同于其他方法,该方法一次只允许移动一行,如果希望移动多个行,可以在beginUpdates-endUpdates中重复调用该函数。
  • -(void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    • 向receiver插入一个或多个sections。
    • 其他说明类似于insertRowsAtIndexPaths:withRowAnimation:
  • -(void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    • 从receiver中删除一个或多个sections。
    • 其他说明类似于insertRowsAtIndexPaths:withRowAnimation:
  • -(void)moveSection:(NSInteger *)section toSection:(NSInteger *)newSection
    • 将table view中的section移动到新位置。若新位置已经有section了,则该section将会向上或向下移动来腾出位置。
    • 可以将该操作与section移动操作和插入删除操作组合进beginUpdates-endUpdates块中,让若有的操作在一个动画中发生。
    • 与插入、删除操作不同,该方法没有动画参数。当section移动时,移动行的动画直接从起始位置到结束为止。另外,不同于其他方法,该方法一次只允许移动一个section,如果希望移动多个sections,可以在beginUpdates-endUpdates中重复调用该函数。

管理table cell的编辑

  • @proper(nonatomic, getter=isEditing) BOOL editing
    • 该属性标识receiver是否处于编辑模式,默认为NO。
    • 在编辑模式中,table的cells可能会在每一个单元的左边显示插入或删除控制,在右边显示一个重排序控制。单击一个控制会导致table view调用data source的方法tableView:commitEditingStyle:forRowAtIndexPath:。
  • -(void)setEditing:(BOOL)editing animated:(BOOL)animate
    • 当调用该方法设置editing属性为YES时,table view通过对每一个可见UITableViewCell对象调用setEditing:animated进入编辑模式;当设置为NO时,将会关闭编辑模式。

重新加载table view

  • -(void)reloadData
    • 重新载入receiver的rows和sections
    • 调用该方法能够载入用于构造table的所有数据,包括cells,section headers和footers,index arrays等。处于效率考虑:table view仅重显示可见的行。
    • 如果重载入的table缩小,会调整offsets。
    • 当希望table view完成重新载入数据时,table view的delegate和data source会调用该方法。
    • 该方法不能在插入、删除操作中调用,特别是在一个beginUpdates-endUpdates块中。
  • -(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    • 重新载入指定行,with指定动画。animation持续影响旧行和新行的方向。例如:如果动画为UITableViewRowAnimationRight,旧行会向右划出,而新行从右边滑入。
    • 该方法的调用会导致table view要求其数据源当前行的新cell
    • 如果希望警告用户一个cell的值发生了改变,可以调用该方法。如果不需要通知用户,只需要改变cell显示的值,可以直接改变值的显示。
  • -(void)reloadSections:(NSIndexSet *)sectoins withRowAnimation:(UITableViewRowAnimation)animation
    • 通过给定动画效果重载指定的sections。
    • 方法的调用会导致table view要求其数据源指定section的新cell
    • table view在新cell插入以及旧cell移除时进行动画。如果希望警告用户一个cell的值发生了改变,可以调用该方法。如果不需要通知用户,只需要改变cell显示的值,可以直接改变值的显示。
  • -(void)relaodSectionIndexTitles
    • 重新载入table view右边的index bar的items。
    • 该方法提供了一种在插入或删除section之后没有重载整个table view时更新section 索引的方式。

访问table view的绘制区域

  • -(CGRect)rectForSection:(NSInteger)section
    • 返回receiver指定section的绘制区域。
  • -(CGRect)rectForRowAtIndexPath:(NSIndexPath *)path
    • 返回由index path指定行的绘制区域
  • -(CGRect)rectForFooterInSection:(NSInteger)section
    • 返回指定section的footer的绘制区域。
  • -(CGRect)rectForHeaderInSection:(NSInteger)section
    • 返回指定section的header的绘制区域。

管理delegate和data source

  • @proper(nonatomic, assign) id<UITableViewDataSource> dataSource
    • receiver table view的数据源对象。该对象必须适应UITableViewDataSource协议(具体将在之后的博文中探讨)。数据源不是保留的(retained)
  • @proper(nonatomic, assign) id<UITableViewDelegate> delegate
    • receiver table view的delegate对象。该对象必须适应UITableViewDelegate协议(具体将在之后的博文中探讨)。数据源不是保留的(retained)

配置table索引

  • @proper(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount
    • table中在右边显示索引链表的行的个数。
    • 该属性只有在UITableViewStylePlain样式下有效,默认为0.
  • @proper(nonatomic, retain) UIColor * sectoinIndexColor
    • table view的索引文本颜色。
  • @proper(nonatomic, retain) UIColor * sectionIndexBackgroudColor
    • 当table view的section索引没有触摸时显示的背景颜色。
    • table view能够在试图的边上显示索引,让用户能够更方便的快速索引table的内容。该属性制定了显示索引的背景颜色。
  • @proper(nonatomic, retain) UIColor * sectionIndexTrackingBackgroudColor
    • table view的索引背景颜色(当用于用手指拖动时)

颜色备注:性能注意触发


0 0
原创粉丝点击