IOS学习笔记(10)UITableView展示数据 cell删除 构建页眉页脚

在TableView中展示分层数据

使用tableViewCells的缩进功能:

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

UITableViewCell *result = nil;

static NSString *MyCellIdentifier = @"SimpleCells";

result = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

if(result == nil){

result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];

}

result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];

result.indentationLevel = indexPath.row;

result.indentationWidth = 10.0f;

return result;

}

每个cell有2个相关属性:缩进等级和缩进宽度,缩进等级与缩进宽度简单相乘,所得结果就是偏移量。


启用TableViewCell的滑动删除


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;

if([tableView isEqual:self.myTableView]){

result = UITableViewCellEditingStyleDelete;

}

return result;

}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];

[myTableView setEditing:editing animated:animated];

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if(editingStyle == UITableViewCellEditingStyleDelete){

if(indexPath.row < [self.arrayOfRows count]){

[self.arrayOfRows removeObjectAtIndexPath.row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];

}

}

}

需要UITableViewDelegate代理协议

tableView:editingStyleForRowAtIndexPath:方法能够启动删除功能,它被tableview调用,同时它的返回值决定了tableView允许用户做什么(插入,删除等),它的返回值说明了表中是否该允许同时插入与删除或者同时不。 tableView:commitEditingStyle:forRowAtIndexPath: 方法实现用户的要求删除。后一种方法在委托中定义,但是它的功能有点重载,不只使用这个方法删除数据,也必须要从表中删除行。


deleteRowsAtIndexPaths:withRowAnimation:方法的第二个参数允许你指定一个动画方法,当行从TableView中删除时这个动画方法会被执行。我们的示例说明了当行被删除时它在从右到左的移动过程中消失。


在TableView中构建页眉和页脚


TableView可以有多个页眉页脚,一个TableView的每个Section都可以有它自己的页眉页脚。

#import <UIKit/UIKit.h>

@interface TableView:UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(monatomic,strong)UITableView *myTableView;

@end


@import "TableView.h"

@implementation TableView

@synthesize myTableView;

- (void)viewDidLoad

{

[super viewDidLoad];

self.title = @"TableView";

self.view.backgroundColor = [UIColor grayColor];

myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

self.myTableView.dataSource = self;

self.myTableView.delegate = self;

self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:myTableView];

}

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

UITableViewCell *result = nil;

static NSString *CellIdentifier = @"CellIdentifier";

result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(result == nil){

result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

result.textLabel.text = [[NSString alloc]initWithFormat:@"Cell %ld",(long)indexPath.row];

return result;

}

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

return 3;

}

/*自定义页眉页脚

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

// UILabel *result = nil;

UIView *result = nil;

if([tableView isEqual:myTableView] && section == 0){

// result = [[UILabel alloc]initWithFrame:CGRectZero];

// result.text = @" Section 1 Header";

// result.backgroundColor = [UIColor clearColor];

// [result sizeToFit];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];

label.text = @"Section 1 Header";

label.backgroundColor = [UIColor clearColor];

[label sizeToFit];

label.frame = CGRectMake(label.frame.origin.x + 10.0f, 5.0f, label.frame.size.width, label.frame.size.height);

CGRect resultFrame = CGRectMake(0.0f, 0.0f, label.frame.size.height, label.frame.size.width + 10.0f);

result = [[UIView alloc]initWithFrame:resultFrame];

[result addSubview:label];

}

return result;

}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

UILabel *result = nil;

if([tableView isEqual:myTableView] && section == 0){

result = [[UILabel alloc]initWithFrame:CGRectZero];

result.text = @" Section 1 Footer";

result.backgroundColor = [UIColor clearColor];

[result sizeToFit];

}

return result;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

CGFloat result = 0.0f;

if([tableView isEqual:myTableView] && section == 0){

result = 30.0f;

}

return result;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

CGFloat result = 0.0f;

if([tableView isEqual:myTableView] && section == 0){

result = 30.0f;

}

return result;

}

*/

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

CGFloat result = 0.0f;

if([tableView isEqual:myTableView] && section == 0){

result = 30.0f;

}

return result;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

CGFloat result = 0.0f;

if([tableView isEqual:myTableView] && section == 0){

result = 30.0f;

}

return result;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值