UITableView详解:8-分段(Section)
在UITableView中,可以设置多个段Section,每个Section中可以包含若干个单元格Cell,同时每个Section还包含了一个头部视图以及一个底部视图,因此具有很强的定制能力。
Section的样例
Section在App中的使用是非常普遍的。例如,在iOS设备的系统设置中UITableView的Section的header和footer通常是显示简单的文字提示,如下图所示;
在实际的App开发中,Section还可以定制成很多复杂的样式,主要体现在对header以及footer的定制,如下图所示。其中的【热卖推荐】、【猜你喜欢】都是对Seciton的headerView的定制。
Section相关的数据源方法
在UITableViewDataSource协议中,如果涉及到多个Section的配置,则需要实现如下方法。如不实现,则seciton的默认值为1,即整个表视图只有一个段Section。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
Section的header/footer的title设置
如果section的header以及footer只显示一些文字提示,则可以直接调用UITableViewDataSource协议中的方法来设置。
- 设置header文字
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- 设置footer文字
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
注意:UITableView的style属性会影响section的header以及footer文字的显示样式,当设置为Grouped时,会显示如下图所示的文字显示效果。
Section的header/footer的自定义样式
通常情况下,我们在开发App的过程中,section的header、footer并不仅仅需要显示一些普通的文字,而是需要对显示的样式进行定制,这就需要通过UITableViewDelegate中定义的代理方法进行定制。
- 定制Section的顶部视图样式
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- 定制Section的底部视图样式
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
Section的索引
在表视图的右侧,可以定制每个section的索引,可以通过如下方法以及属性来设置。例如,在通讯录应用中,右侧的索引可以显示联系人的姓名拼音首字母。
- 索引显示的内容需要通过sectionIndexTitlesForTableView:数据源方法来实现,索引内容需要存放在一个数组中作为该方法的返回值,数组中的每个对象都与一个Section对应。
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- 索引文字颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor;
- 索引背景颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor ;
- 索引点击时的背景颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor;
文章发布时间为: November 7th , 2016 at 11:17 pm
最后编辑时间为: September 13th , 2017 at 02:13 pm
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 13th , 2017 at 02:13 pm
本文由 99ios 创作,转载请注明出处