您的位置:首页 > 产品设计 > UI/UE

UITableView(三)--- UITableViewDataSource

2014-07-27 11:05 113 查看

基本概念

data source(数据源)为table view对象构造和修改table view提供了信息基础。
作为数据模型的代表,数据源提提供了用于视图显示的最小信息。
协议的必须方法提供了table-view要显示的cell,一级通知tableView对象有多少section以及一个section有多少行。(粉红色背景的表示必须实现的方法)
协议的可选择方法用于配置table view的插入、删除和重排序行等操作。
【注】如果希望实现滑动删除特征(即:用户水平滑动导致显示删除按钮)时,必须实现tableView:commitEditingStyle:forRowAtIndexPath:方法

Task

配置table view

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath
将cell插入到table view的特殊位置。插入成功,则返回table view用于指定行的cell,否则返回nil。
返回的UITableVIewCell对象因为性能原因频繁进行重用。你应该通过发送dequeueReusableCellWithIdentifier:方法从tableView中取得一个之前创建的标记为可重用的cell。
table cell的许多属性基于它是否是一个分隔符,在数据源提供的信息上动态进行设置。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
返回table view的section个数。tableView section数的默认值为1。

-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSecion:(NSInteger)section
返回指定section的行的个数。

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
返回tableView中section的titles。
table view中section的title保存在字符串数组中,并在table view的右侧以index 链表的形式显示。这种方式显示的table view必须是plain样式。例如:对于字母链表,应该返回包含A-Z的数组。

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)titleatIndex:(NSInteger)index
返回拥有指定title的section索引。该方法传入了一个索引号(由sectionIndexTitlesForTableView:方法返回的数组中的section title的索引号)和section索引链表中的title项。
这里有两个索引号:
由sectionIndexTitlesForTableView:方法返回的数组中的section title的索引号。---- 传入参数

table view中section得索引。--- 返回值

只有当table view有section索引链表时才实现该方法,此时table view只能创建为UITableViewStylePlain形式。
【注】:由sectionIndexTItlesForTableView:返回的section title数组可能比table view中section数的实际items要少。

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回table view中指定section的header的title。
table view中对header的title使用固定的字体,若希望不同的字体,需要通过delegate的tableView:viewForHeaderInSection:方法返回一个custom view(例如:UILabel对象)进行替代

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
返回table view中指定section的footer的title。
table view中对footer的title使用固定的字体,若希望不同的字体,需要通过delegate的tableView:viewForFooterInSection:方法返回一个custom view(例如:UILabel对象)进行替代。

插入/删除table的行

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowIndexPath:(NSIndexPath *)indexPath
让数据源提交指定行的插入或删除操作。
常用的editingStyle为:UITableViewCellEditingStyleInsert和UITableViewCellEditingStyleDelete
当用户单击table view中UITableViewCell的插入(绿色加好)控制或者删除按钮时,table view将会向数据源发送该消息,让其提交修改。如果用户单击删除(红色减号)控制,table view会显示删除按钮获取信息。
数据源通过触发UITableView的insertRowAtIndexPaths:withRowAnimation:或者deleteRowsAtIndexPaths:withRowAnimation:方法提交插入和删除操作。
若要时滑动删除可用,则必须实现这个方法。在实现方法内,应该调用:setEditing:animated:方法。
如果因为特殊原因必须延迟调用该方法,可以实现peformSelector:withObject:afterDelay:方法

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
当前的给定行是否可编辑。YES:可编辑;NO:不可编辑

重排序table的行

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndex:(NSIndexPath *)indexPath
询问数据源是否能讲tableView指定位置的行进行移动。
该方法允许数据源指定指定行的重排序控制不显示。默认情况下,重排序控制在实现了tableView:moveRowAtIndexPath:toIndexPath方法时显示。

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPathtoIndexPath:(NSIndexPath *)toIndexPath
告诉数据源在指定位置(fromIndexPath)的行即将移动到toIndexPath位置。
当用户在fromRow上选择reorderControl时,UITableView将会向数据源发送该消息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息