如何修复tableview底部的按钮?

时间:2016-04-21 12:02:21

标签: ios objective-c uitableview uibutton tablefooterview

在我的应用程序中,我想修复表格视图底部的按钮。

这是我的初始屏幕,

enter image description here

在页脚部分创建的按钮

-(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;
}

之后我会这样,

enter image description here

我该如何解决?

4 个答案:

答案 0 :(得分:2)

为什么你不做这样的事情?

-(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;

}

并在初始化表格后调用此方法

答案 1 :(得分:0)

-(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

之后

答案 2 :(得分:0)

如果您想要使用tableview滚动页脚/页眉滚动,则需要使用分组的样式表视图。

[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]

分组样式表视图将具有默认的backgroundColor和section header / footer height。您需要明确设置这些值。

如果您只有一个部分,最好使用tableFooterView而不是部分页脚视图

答案 3 :(得分:0)

滚动该按钮可见意味着,UITableview单元格可以重复使用,以便按钮可用,您需要做的就是。

1.在UITableView中获取行数,

然后在indexpath的行的单元格内:

Total number of row ==indexpath.row
{
   //inside this button.hidden=no;
}
else
{
   //button.hidden=yes;
}