在tableview页脚中添加按钮 [英] Add button in tableview footer
问题描述
我在一个viewcontroller中有tableview。我有一节。我想在页脚中添加按钮。我已编写此代码,但页脚视图未显示。
I have tableview in one viewcontroller. I have one section in that. I want to add button in footer. I have written this code but footer view is not displaying.
- (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的高度。
I have also set height of footer 100 in its delegate method.
截图:
最初我有3个数据。但是当我搜索特定数据并且结果将在当时的tableview中显示时,我有一个数据,因此当时页脚位置发生了变化。我想在最初的地方修剪页脚。
screenshots: Initially i have 3 data. But when I search for particular data and result will be displayed in tableview at that time I have one data so at that time footer location changed. I want to to fix footer at initial place.
< img src =https://i.stack.imgur.com/MEe1r.pngalt =在此处输入图像说明>
编辑:
作为替代解决方案,可以通过在末尾添加额外的单元格来添加按钮。
As an alternative solution one can add button by adding extra cell at the end.
推荐答案
每 Apple的文档你必须还实现 heightForFooterInSection
方法,否则你的 viewForFooterInSection
不会这样做任何事情。
Per Apple's Docs you must also implement the heightForFooterInSection
method, otherwise your viewForFooterInSection
wouldn't do anything.
- (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");
}
这篇关于在tableview页脚中添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!