IOS 9 UITableView整理

UITableView详解
本文详细介绍了UITableView的两种风格及单元格样式,讲解了UITableViewDataSource和UITableViewDelegate协议的实现方法,包括必须和可选的方法。此外,还提供了自定义单元格、设置编辑操作及索引等实用技巧。

首先,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。

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

必须要实现的有两个

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

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

可选的有

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

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

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

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

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

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

- (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];


转载于:https://my.oschina.net/magina/blog/530079

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值