第三周的课程终于迎来了大杀器——tableView。table这个view可能是使用面最广泛的一个数据展示方法了吧。本文就简要的介绍一下tableview在iOS开发过程中的使用。
建立
一个tableView要想顺利的现实在iOS的VC当中,必须要实现两个协议,他们分别是:UITableViewDataSource,UITableViewDelegate。
其中UITableViewDataSource负责Table的数据供应,UITableViewDelegate负责对Table的各种事件进行相应。
Table主要由两个部分组成section和cell(或者称为row)。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicGcq5CUVFVZflDNwADMyETMvwFNwYTMwIzLcdWbp9CXzRWYvxGc19CX0VmbuEmbph2Yz9mLjlGdhR3cvw1LcpDc0RHaiojIsJye.jpg)
- UITableViewDataSource
//在UITableViewDataSource中有两个方法是必须实现的 //返回某个section中的行数,应由数据进行指定 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; //根据indexPath(包括section和row)来返回对应的cell实例 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; //非必须 //返回表格的section数量,下标从0开始计算,默认为1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // cell是否可以进入编辑模式,默认为true - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; //返回section的索引,主要用于通讯录类表格(e.g. "ABCD...Z#") - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView ; //返回对应section的编号 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // Data manipulation - insert and delete support // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- UITableViewDelegate
delegate中对table的各项操作定义了非常详细的操作,在这里仅简要介绍。//在delegate中没有必须实现的方法,但是有几个非常常用的方法 //设置section的header和footer的view - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; //点击操作响应方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Cell定制
在tableView中,最重要的就是定制cell,用于显示数据。在这里简要的说明几种cell的产生方法。
- 使用系统预定义的cell
initWithStyle:UITableViewCellStyle的几种值://先根据Identifier取对象实例,实现复用 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@""]; //如果为空进行创建 if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Default"]; }
-
UITableViewCellStyleDefault
该格式提供了一个简单的左对齐的文本标签textLabel和一个可选的图像imageView。如果显示图像,那么图像将在最左边。这种格式虽然可以设置detailTextLabel,但是不会显示该标签。
-
UITableViewCellStyleSubtitle
该格式与前一种相比,增加了对detailTextLabel的支持,该标签将会显示在textLabel标签的下面,字体相对较小。
-
UITableViewCellStyleValue1
该格式居左显示textLabel,居右显示detailTextLabel,且字体较小
-
UITableViewCellStyleValue2
该格式居左现实一个小型蓝色主标签textLabel,在其右边显示一个小型黑色副标题详细标签detailTextLabel。该格式不支持图像.
GeekBand.iOS-UITableView
-
- 自定义Cell
-
使用StoryBoard绘制
在SB中使用IB对cell进行绘制,并指定identifier,内部的view指定tag.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@""]; UIView* view =(UIView *) [cell viewWithTag:int];
-
xib方式
创建一个XibCell继承UITableViewCell,并创建xib文件,在xib中对cell进行绘制
//0.覆盖initWithStyle方法,手工加载xib //1.在VC的viewDidLoad方法中对xib进行注册 //2.使用Identifier创建Cell实例
-
使用UITableViewController设计静态表格
对类似于系统设置这些界面,使用静态表格直接完成设计
-
TableView交互
- 选中
GeekBand.iOS-UITableView - 控制表格滚动
GeekBand.iOS-UITableView - 刷新table
//重载所有数据 [tableView reloadData]; //刷新行 [tableView reloadRowsAtIndexPaths:withRowAnimation]; //刷新组 [tableView reloadSections:WithAnimation]; //刷新组索引 [tableView reloadSectionIndexTitles];
- 编辑模式
GeekBand.iOS-UITableView GeekBand.iOS-UITableView -
表格与搜索
搜索并非表格的一部分,但实际使用中经常联系在一起。
- UISearchBar(iOS2+)
[tableView setTableViewHeaderView:searchBar];
- UISearchDisplayController(iOS7)
- UISearchController(iOS8)
- UISearchBar(iOS2+)
- 高亮与菜单
GeekBand.iOS-UITableView - 表格索引
GeekBand.iOS-UITableView
数据传递
和其它VC一样使用segue或delegate
转载于:https://my.oschina.net/u/1448851/blog/648754