首页 > 移动平台 > 详细

IOS 9 UITableView整理

时间:2015-11-13 19:30:52      阅读:353      评论:0      收藏:0      [点我收藏+]

首先,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];


IOS 9 UITableView整理

原文:http://my.oschina.net/magina/blog/530079

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!