IOS 9 UITableView整理

首先,UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。微信

而后,UITableViewCellStyle的样式代理

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,   // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)    
    UITableViewCellStyleValue1,    // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)    
    UITableViewCellStyleValue2,    // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)    
    UITableViewCellStyleSubtitle   // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)};

固然也能够自定义TableViewCell。code

要使用UITableView必需要实现两个协议UITableViewDataSource和UITableViewDelegate,并实现必要的代理方法。索引

必需要实现的有两个事件

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

这两个方法分别返回每组中的行数,所展现的tableviewcellit

可选的有io

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //tableview总共多少组,默认为1
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

上面两个方法返回每组头部和尾部的文本,前提是实现了另外三个方法,才能展示,下面三个方法是用来分别设置每一行,每组头部,每组尾部的高度table

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

若是只是设置title过于简单,还能够自定义view加入头部和尾部,以下面两个方法class

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;  
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

UITableView的每一行点中以后有相应的响应事件,经过实现下面的方法实现select

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView默认还能够想微信同样编辑每一行,经过两个方法实现

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;

第一个用来设置是否可编辑,默认是YES,第二个用来设置能够怎么编辑cell,以下示例代码

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"1111");
    }];
    topRowAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"2222");
    }];
    deleteRowAction.backgroundColor =[UIColor redColor];
    return @[topRowAction, deleteRowAction];
}



选中每一个cell时默认会有选中状态,以下结构体:

typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle

若是不须要选中状态,能够设置为

UITableViewCellSelectionStyleNone

固然也能够在下面方法中设置

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



UITableView支持设置索引,只要在如下方法中返回每组的index就行

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView



要让UITableView展现数据,就必要在数据源到位后调用reloadData方法,会刷新整个tableview,固然也能够单独刷新某一组或者某一行,代码以下

//section
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:index];
[tableview reloadSections:indexSetwithRowAnimation:UITableViewRowAnimationAutomatic];
//cell 
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:section];   
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
相关文章
相关标签/搜索