UITableViewDataSource

来源:互联网 发布:python 建立tcp连接 编辑:程序博客网 时间:2024/06/05 23:54

UITableViewDataSource IOS9.1

需要实现的:

设置每组的行数

- ()NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

设置单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

可选的:

设置组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

设置每组的组头文字

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

设置每组的组尾文字

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

设置单元格是否可被编辑

*这里设置了 false ,实现了commitEditingStyle方法,也不会出现左划删除按钮- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

设置单元格是否可被移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;*这个需要tableview处于编辑状态, [_tableView setEditing:true];*记得将当前单元格的数据删除并插入到之后所在位置

设置表单的索引

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView 

处理点击索引的操作

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index *例如:- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{    NSString *key = [keys objectAtIndex:index];    NSLog(@"sectionForSectionIndexTitle key=%@",key);    if (key == UITableViewIndexSearch) {        [tableView setContentOffset:CGPointZero animated:NO];        return NSNotFound;    }    return index;}

实现这个方法 可以出现单元格左划删除按钮

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;*如果要删除数据,需要在方法体内再判断,如:- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    NSMutableArray *group =_tableViewDataArr[indexPath.section];    NSMutableDictionary *contact = group[indexPath.row];    if(editingStyle == UITableViewCellEditingStyleDelete){        NSLog(@" 删除单元格 ");        [group removeObject:contact];        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];    }}

处理单元格移动操作

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;*这个需要tableview处于编辑状态,
0 0