繁体   English   中英

如何修复tableview底部的按钮?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM