[英]Add UIButton to UITableView programmatically but the button doesn't show
I'd like to add a button above table view, not scrolled with the table view. 我想在表格视图上方添加一个按钮,而不是与表格视图一起滚动。 But the button doesn't show.
但是该按钮没有显示。 Any ideas?
有任何想法吗?
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];
Thanks in advance! 提前致谢!
You cannot do this with a UITableViewController
. 您不能使用
UITableViewController
做到这一点。 The reason: the view
property of the view controller is the table view, so you cannot add anything above it. 原因:视图控制器的
view
属性是表视图,因此您不能在其上方添加任何内容。
The way to do this is to use a plain UIViewController
and add your button and a UITableView
as subviews (better in Interface Builder than in code, BTW). 做到这一点的方法是使用普通的
UIViewController
并将按钮和UITableView
添加为子视图(在Interface Builder中比在代码中更好,BTW)。
In order to make the table view work as expected you have to add 为了使表视图按预期工作,您必须添加
UITableViewDataSource
and UITableViewDelegate
protocols UITableViewDataSource
和UITableViewDelegate
协议 UITableViewController
, implement at least the necessary datasource methods UITableViewController
,至少实现必要的数据源方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.