反正有没有用一个通用页脚创建一个UITableView?

我正在尝试创建一个包含多个部分的UITableView 。 每个部分都有自己的标题,但我试图为整个表视图制作一个通用的页脚,它保持在一个位置……

使用UITableViewDelegate方法可以实现这种逻辑吗? 或者我应该创建一个自定义视图,然后尝试将其作为子视图添加到我的表视图中? 我目前的页脚包含一个UIButton

如果有人有一些很棒的示例代码。

编辑:此问题与引用的问题不同。 我试图制作一个浮动在UITableView上方的通用页脚。 另一个问题没有指定页脚的位置,只需要一个页脚。

阅读有关UITableView的tableFooterView属性的信息。

现在一些代码:(使用ARC)

 UILabel *footer = [UILabel alloc] init]; footer.text = @"Some text" ; footer.backgroundColor = [UIColor greenColor]; self.tableView.tableFooterView = footer; 

现在在整个tableview的底部有一个绿色的UILabel,带有一些文本。

我最终创建了一个子视图并将我的按钮添加到该视图中。 然后我把这个观点视为我的“页脚”。

这是给我预期结果的代码。

 - (void)viewDidLoad { [super viewDidLoad]; //self.tableView.delegate = self; //self.tableView.dataSource = self; //save current tableview, then replace view with a regular uiview self.tableView = (UITableView*)self.view; UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame]; self.view = replacementView; [self.view addSubview:self.tableView]; UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)]; //create the button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //button.userInteractionEnabled = YES; //the button should be as big as a table view cell //width of the button can be set but the width of the view it is added to will always match the width of the tableView [button setFrame:CGRectMake(60, 0, 200, 45)]; //set title, font size and font color [button setTitle:@"Build" forState:UIControlStateNormal]; [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //set action of the button [button addTarget:self action:@selector(buildThenSegue) forControlEvents:UIControlEventTouchUpInside]; //add the button to the view [footerView addSubview:button]; footerView.userInteractionEnabled = YES; [self.view addSubview:footerView]; self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 

}

请注意,这是在UITableViewController的子类中。

我引用了另一个问题的答案: https : //stackoverflow.com/a/9084267/1091868