`
yidongkaifa
  • 浏览: 4062889 次
文章分类
社区版块
存档分类
最新评论

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;

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics