调整uitableview部分页脚的高度

时间:2014-04-12 16:59:24

标签: ios uitableview footer

我刚刚开始使用UITableView部分页脚,但是遇到了一些问题。所以我有一个UITableView,其底部使用页脚附加了一个编辑按钮。我的第一个问题是当你点击那个按钮时,页脚的大小应该加倍,因为编辑按钮会变成一个添加按钮和一个完成按钮,而不是一个按钮。什么是使页脚大小更大的最佳方法。我尝试通过self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88);更改页脚的框架,但这不起作用。

我的第二个问题是我希望页脚始终与表格一起滚动,就像它只是另一个单元格一样。它应该只是位于桌子的底部,无论如何。如果用户向下滚动,它不应该停在屏幕的底部并且坚持,它应该继续滚动与tableview的其余部分。我该如何解决这个问题?

以下是我创建页脚的方法:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 48;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    //view.backgroundColor = [UIColor grayColor];

    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(0, 0, 320, 44);
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateHighlighted];
    [self.addButton addTarget:self action:@selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.hidden = YES;
    [view addSubview:self.addButton];

    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton.frame = CGRectMake(0, 0, 320, 44);
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editButton addTarget:self action:@selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.editButton];

    self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editDoneButton addTarget:self action:@selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
    self.editDoneButton.hidden = YES;
    [view addSubview:self.editDoneButton];

    self.editLabel = [[UILabel alloc] init];
    self.editLabel.frame = CGRectMake(10, 5, 100, 30);
    self.editLabel.text = @"Edit";
    self.editLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    [view addSubview:self.editLabel];

    self.addLabel = [[UILabel alloc] init];
    self.addLabel.frame = CGRectMake(10, 5, 100, 30);
    self.addLabel.text = @"Add";
    self.addLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.addLabel.hidden = YES;
    [view addSubview:self.addLabel];

    self.editDoneLabel = [[UILabel alloc] init];
    self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
    self.editDoneLabel.text = @"Done";
    self.editDoneLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.editDoneLabel.hidden = YES;
    [view addSubview:self.editDoneLabel];

    self.theLine = [[UIView alloc] init];
    self.theLine.frame = CGRectMake(0, 0, 320, .5);
    UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
    self.theLine.backgroundColor = borderColor;
    [view addSubview:self.theLine];

    return view;
}

修改

考虑到下面的答案之后,这就是我已经切换到的,但现在我的页脚在那里,只有一个灰色的矩形。

以下是我现在使用的代码:

-(void)setUpFooter {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
    view.backgroundColor = [UIColor redColor];

    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(0, 0, 320, 44);
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"Add_Class_Button"] forState:UIControlStateHighlighted];
    [self.addButton addTarget:self action:@selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.hidden = YES;
    [view addSubview:self.addButton];

    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton.frame = CGRectMake(0, 0, 320, 44);
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editButton addTarget:self action:@selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.editButton];

    self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateNormal];
    [self.editDoneButton setImage:[UIImage imageNamed:@"Edit_Classes_Button"] forState:UIControlStateHighlighted];
    [self.editDoneButton addTarget:self action:@selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
    self.editDoneButton.hidden = YES;
    [view addSubview:self.editDoneButton];

    self.editLabel = [[UILabel alloc] init];
    self.editLabel.frame = CGRectMake(10, 5, 100, 30);
    self.editLabel.text = @"Edit";
    self.editLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    [view addSubview:self.editLabel];

    self.addLabel = [[UILabel alloc] init];
    self.addLabel.frame = CGRectMake(10, 5, 100, 30);
    self.addLabel.text = @"Add";
    self.addLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.addLabel.hidden = YES;
    [view addSubview:self.addLabel];

    self.editDoneLabel = [[UILabel alloc] init];
    self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
    self.editDoneLabel.text = @"Done";
    self.editDoneLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    self.editDoneLabel.hidden = YES;
    [view addSubview:self.editDoneLabel];

    self.theLine = [[UIView alloc] init];
    self.theLine.frame = CGRectMake(0, 0, 320, .5);
    UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
    self.theLine.backgroundColor = borderColor;
    [view addSubview:self.theLine];

    view.userInteractionEnabled = YES;
    self.classTableView.tableFooterView = view;
    self.classTableView.tableFooterView.userInteractionEnabled = YES;

}

2 个答案:

答案 0 :(得分:0)

希望我能帮忙。

要设置节页脚高度,还必须更改方法heightForFooterInSection中的返回值。要重新加载高度,您可以拨打[tableView reloadData][tableView reloadSections:...]

表格页脚始终粘贴在屏幕底部,但是有一个不同的视图用于在表格视图之后放置内容。 tableView.tableFooterView是位于表视图之后的视图。更改此项以在表视图后添加元素。

干杯!

答案 1 :(得分:0)

UITableView中有两种类型的页脚。桌子底部有桌面页脚,每个部分底部有桌面页脚。

听起来您想要使用表部分标头。但是,您的代码段是设置表格页脚的框架,而不是表格页脚。

self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88); 

如果要调整表格部分页脚高度,tableView:heightForFooterInSection:方法将需要返回不同的值。