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

UI -- UITableView 数据源协议与委托协议

2016-07-13 11:22 393 查看
与UIPickerView等复杂控件类似,表视图在开发过程中也会使用委托协议和数据源协议,而表视图UITableView的数据源协议是UITableViewDataSource,委托协议是UITableViewDelegate。UITableViewDataSource协议中的主要方法如下所示,其中必须要实现的方法有tableView:numberOfRowsInSection:和tableView:cellForRowAtIndexPath:。

UITableViewDataSource协议的主要方法:

/**
*  为表视图单元格提供数据,该方法是必须要实现的方法
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { }

/**
*  返回某个节中的行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { }

/**
*  返回某个节头的标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { }

/**
*  返回节脚的标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { }

/**
*  返回节的个数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { }

/**
*  提供表视图节索引标题
*/
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { }

/**
*  为删除或修改提供数据
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { }


UITableViewDelegate协议主要用来设定表视图中节头和节脚的标题,并响应-些动作事件,主要的方法如下,他们都是可选择的

UITableViewDelegate协议的主要方法

/**
*  为节头准备自定义视图,iOS6之后可以使用UITableViewHeaderFooterView
*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { }

/**
*  为节脚准备自定义视图,iOS6之后可以使用UItableViewHeaderFooterView
*/
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { }

/**
*  该方法在节头从屏幕中消失时触发
*/
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {

}

/**
*  当节脚从屏幕中消失时触发
*/
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section {

}

/**
*  当单元格从屏幕中消失时触发
*/
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

}

/**
*  响应选择表视图单元格时调用的方法
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

/**
*  响应沿单元格水平滑动时间(iOS8之后的方法)
*/
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息