在tableview页脚中添加button

我有一个viewcontroller中的tableview。 我有一节。 我想在页脚中添加button。 我已经写了这个代码,但页脚视图不显示。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom]; [addcharity setTitle:@"Add to other" forState:UIControlStateNormal]; [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside]; addcharity.frame=CGRectMake(0, 0, 40, 30); [footerView addSubview:addcharity]; return footerView; } 

我也在其代表方法中设置页脚100的高度。

屏幕截图:最初我有3个数据。 但是,当我search特定的数据和结果将显示在tableview当时我有一个数据,所以当时脚注位置改变。 我想在最初的地方修复页脚。 在这里输入图像说明

在这里输入图像说明

编辑:

作为替代解决scheme,可以通过添加额外的单元格来添加button。

每苹果的文档,你还必须实现heightForFooterInSection方法,否则你的viewForFooterInSection不会做任何事情。

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 100.0f; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if(tableView == myListTableview) //Here you can make decision { UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom]; [addcharity setTitle:@"Add to other" forState:UIControlStateNormal]; [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside]; [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //Set the color this is may be different for iOS 7 addcharity.frame=CGRectMake(0, 0, 130, 30); //Set some large width for your title [footerView addSubview:addcharity]; return footerView; } } - (void)addCharity:(id)sender { NSLog(@"add to charity"); } 

同样的代码在迅速

 func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 100.0 } func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let footerView = UIView(frame:CGRectMake(0,0,320,40)) let loginButton = UIButton(type: .Custom) loginButton.setTitle("LOGIN", forState: .Normal) loginButton.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside) loginButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) loginButton.backgroundColor = UIColor.blueColor() loginButton.frame = CGRectMake(0, 0, 130, 30) footerView.addSubview(loginButton) return footerView } func loginAction() { print("Hello"); } 

确定三件事

1-您不执行此方法

 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 

2-这种方法

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

是一个UITableViewDelegate方法,而不是UITableViewDataSource,检查tableview的委托是否设置与控制器

3-确保你正在实施这个方法

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

最后 ,如果一切正常,仍然不能正常工作,那么你可以添加表中的“tableFooterView”属性的视图作为整个表的页脚,而不仅仅是部分