繁体   English   中英

以编程方式将UIButton添加到UITableView中,但按钮不显示

[英]Add UIButton to UITableView programmatically but the button doesn't show

我想在表格视图上方添加一个按钮,而不是与表格视图一起滚动。 但是该按钮没有显示。 有任何想法吗?

UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setImage:[UIImage imageNamed:@"refresh"] forState:UIControlStateNormal];
CGRect applicationFrame = [UIScreen mainScreen].bounds;
CGFloat screenWidth = CGRectGetWidth(applicationFrame);
CGFloat screenHeight = CGRectGetHeight(applicationFrame);
CGFloat buttonWidth = 40.0f;
CGFloat buttonHeight = 40.0f;
CGFloat spacing = 10.0f;
CGFloat buttonX = screenWidth - buttonWidth - spacing;
CGFloat buttonY = screenHeight - buttonHeight - spacing;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
refreshButton.frame = buttonFrame;
[refreshButton addTarget:self
                  action:@selector(clickRefreshButton)
        forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:refreshButton];

提前致谢!

您不能使用UITableViewController做到这一点。 原因:视图控制器的view属性表视图,因此您不能在其上方添加任何内容。

做到这一点的方法是使用普通的UIViewController并将按钮和UITableView添加为子视图(在Interface Builder中比在代码中更好,BTW)。

为了使表视图按预期工作,您必须添加

  • 表格视图的插座
  • 采用UITableViewDataSourceUITableViewDelegate协议
  • 与在UITableViewController ,至少实现必要的数据源方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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