一、UITableViewDataSource的方法
// 必须要实现的方法
@required
// UITableView可以存在多个组,这个方法返回指定组(section)中有多少条记录
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 这个方法返回的是每一个行的布局,数据也要在这里面绑定,如果要自定义布局,则这个方法返回自定义的UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 可选方法
@optional
// 此方法返回当前UITableView有多少个组,如果不改写这个方法,则默认值为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// 这个方法返回开头的组名,字体样式是固定的,如果要改样式,则要改写UITableViewDelegate中的viewForHeaderInSection方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 这个方法返回组结尾时显示的名字,如上图中的fone, ftwo
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// Editing
// 返回row是否可以编辑,默认为可以编辑,此方法一般不用
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回row是否可以移动,在UITableView要实现一些动画时,会用到,此方法一般不用
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回section的titles列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
// tell table which section corresponds to section title/index (e.g. "B",1))
- (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
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// Data manipulation - reorder / moving support
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
二、UITableViewDelegate常用方法
// 这个方法返回每个UITableViewCell的高度,如果要改变cell的高度,要在这里返回修改后的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 当点击某个cell时触发此方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
三、点击cell后放开时自动取消选中
在 didSelectRowAtIndexPath方法中,调用 deselectRowAtIndexPath方法取消选中即可。
代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 取消选中
[self.tableViewdeselectRowAtIndexPath:indexPath animated:YES];
}
四、一些实用的小知识点
1、去除cell之间的间隔线(气泡聊天的时候会用到)
tableView.separatorStyle = NO;