[英]How to fix a button at the bottom of the tableview?
在我的應用程序中,我想修復表格視圖底部的按鈕。
這是我的初始屏幕,
在頁腳部分創建的按鈕
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(tableView == educateTbl)
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
return footerView;
}
return nil;
}
之后我就變成這樣了
我該如何解決?
你為什么不做這樣的事情?
-(void)setTableFooter
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
tableView.tableFooterView = footerView;
}
並在初始化表后調用它
-(void)CreateCustomFooter
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
tableView.tableFooterView = footerView;
}
將上述方法放在 viewDidLoad() 方法中並在初始化 tableview 之后
如果您想使用 tableview 滾動部分頁腳/頁眉,則需要使用分組樣式的 table view。
[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]
分組樣式表視圖將具有默認的 backgroundColor 和節頁眉/頁腳高度。 您需要明確設置這些值。
如果只有一節,最好使用tableFooterView
而不是節頁腳視圖
雖然滾動該按鈕可見意味着,UITableview 單元格是可重用的,以便該按鈕可用,您必須做的事情意味着。
1.在 UITableView 中取行數,
然后在 indexpath 行的單元格內:
Total number of row ==indexpath.row
{
//inside this button.hidden=no;
}
else
{
//button.hidden=yes;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.