TableView(2)

1.启用tableView的滑动删除 (想让你的用户能从TableView中轻松删除行)

方案:在delegate中实现tableView:editingStyleForRowAtIndexPath:方法,在data source中实现tableView:commitEditingStyle:forRowAtIndexPath: 方法

代码:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;
    if ([tableView isEqual:_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 removeObjectAtIndex:indexPath.row];
            //然后删除表视图的cell
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

tableView:editingStyleForRowAtIndexPath:方法能够启动插入删除功能,它被tableView调用,同时它的返回值决定了tableView允许用户做什么操作(插入,删除等)

tableView:commitEditingStyle:forRowAtIndexPath: 必须使用该方法删除数据,也必须要从表中删除行.

deleteRowsAtIndexPaths:withRowAnimation:  实现表视图删除行,第二个参数是删除行时的动画方案

 

2.在Tableview中构建页眉和页脚

方案:创建一个视图,然后把这个视图分配到tableView 的1个Section的页眉和页脚中.你也可以指定某个页眉或者页脚的高度.

代码:

    我们先创建一个带有tableView 的简单APP.随后我们提供2个标签,它们属于UILable类;一个做页眉,一个做页脚.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:_myTableView];
}
- (BOOL)shouldAutorotate{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (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 stringWithFormat:@"Cell %ld",indexPath.row];
    return result;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

然后用UITableViewDelegate定义的2个重要方法来提供一个页眉标签和另一个页脚标签:

tableView:viewForHeaderInSection:

tableView:viewForFooterInSection:

另外我们还能规定页眉页脚的高度,让表视图跟美观,方法:

tableView:heightForHeaderInSection: 

tableView:heightForFooterInSection: 

下面我们实现这些方法:

#pragma mark - 添加页眉和页脚
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *result = nil;
    if ([tableView isEqual:_myTableView] && section == 0) {
        result = [self createLabelWithString:@"Section 1 Header"];
    }
    return result;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UILabel *result = nil;
    if ([tableView isEqual:_myTableView] && section == 0) {
        result = [self createLabelWithString:@"Section 1 Footer"];
    }
    return result;
}
- (UILabel *)createLabelWithString:(NSString *)string{
    UILabel *result = [[UILabel alloc]initWithFrame:CGRectZero];
    result.text = string;
    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;
}

此时运行程序,会发现页眉页脚视图label 放在了最左面.你可能想通过更改页眉标签的frame解决这个问题,但这个方法行不通.

解决方法是 创建一个通用的UIView,把页眉页脚标签放上面.此时再更改页眉页脚的位置.

修改tableView:viewForHeaderInSection: 和 tableView:viewForFooterInSection:方法的实现过程:

#pragma mark - 添加页眉和页脚
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *result = nil;
    CGFloat offsetX = 50.0f;
    if ([tableView isEqual:_myTableView] && section == 0) {
        UILabel *label = [self createLabelWithString:@"Section 1 Header"];
        label.frame = CGRectMake(label.frame.origin.x + offsetX, 5.0f, label.frame.size.width, label.frame.size.height);
        CGRect resultFrame = CGRectMake(0.0f, 0.0f, label.frame.size.width + offsetX, label.frame.size.height);
        result = [[UIView alloc]initWithFrame:resultFrame];
        [result addSubview:label];
    }
    return result;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *result = nil;
    CGFloat offsetX = 50.0f;
    if ([tableView isEqual:_myTableView] && section == 0) {
        UILabel *label = [self createLabelWithString:@"Section 1 Footer"];
        label.frame = CGRectMake(label.frame.origin.x + offsetX, 5.0f, label.frame.size.width, label.frame.size.height);
        CGRect resultFrame = CGRectMake(0.0f, 0.0f, label.frame.size.width + offsetX, label.frame.size.height);
        result = [[UIView alloc]initWithFrame:resultFrame];
        [result addSubview:label];
    }
    return result;
}

根据以上了解到的方法,我们可以放置图片做页眉页脚.

另外,如果想放入文字作为页眉页脚,可以使用2个更简单的方法:

tableView:titleForHeaderInSection: 

tableView:titleForFooterInSection: 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"Section 1 Header";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NSString *result = nil;
    if ([tableView isEqual:_myTableView] && section == 0) {
        result = @"Section 1 Footer";
    }
    return result;
}

 

posted @ 2014-10-23 10:55  safiri  阅读(234)  评论(0编辑  收藏  举报