问题描述
我需要在动态填充的UIViewTable上方放置一个按钮。不填充第一个单元格(第0行),而是利用标题区域是正确的,所以我使用UITableViewDelegate方法以编程方式创建包含UIButton的UIView:
I need to place a button immediately above a dynamically populated UIViewTable. It feels right to not populate the first cell (row 0), but rather utilize the header area, so I use the UITableViewDelegate method to programmatically create a UIView containing a UIButton:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; // x,y,width,height
UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportButton.frame = CGRectMake(80.0, 0, 160.0, 40.0); // x,y,width,height
[reportButton setTitle:@"rep" forState:UIControlStateNormal];
[reportButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:reportButton];
return headerView;
}
但是,如下图所示,按钮没有给出必要的空间(I期望标题的高度遵守44参数。)
However, as depicted below, the button is not given the necessary space (I expected the height of the header to adhere to the 44 argument).
这里有什么问题?我应该补充一点,UITableView是在一个单独的XIB文件中创建的。
What is wrong here? I should add that the UITableView is created in a separate XIB-file.
推荐答案
您还必须实现 tableView:heightForHeaderInSection :
You also have to implement tableView:heightForHeaderInSection:
on your table view delegate.
这篇关于在UITableView标头的标头中添加UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!