ios 如何更改分组 UITableView 标题的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17699831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change height of grouped UITableView header?
提问by circuitlego
I know how to change the height of the section headers in the table view. But I am unable to find any solution to change the default spacing before the first section.
我知道如何更改表格视图中节标题的高度。但是我找不到任何解决方案来更改第一部分之前的默认间距。
Right now I have this code:
现在我有这个代码:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0){
return 0;
}
return 10;
}
采纳答案by circuitlego
It appears that I can't set a table header view with height of 0. I ended up doing the following:
看来我无法设置高度为 0 的表格标题视图。我最终执行了以下操作:
- (void)viewWillAppear:(BOOL)animated{
CGRect frame = self.tableView.tableHeaderView.frame;
frame.size.height = 1;
UIView *headerView = [[UIView alloc] initWithFrame:frame];
[self.tableView setTableHeaderView:headerView];
}
回答by Mark Krenek
Return CGFLOAT_MIN
instead of 0 for your desired section height.
CGFLOAT_MIN
对于所需的截面高度,返回而不是 0。
Returning 0 causes UITableView to use a default value. This is undocumented behavior. If you return a very small number, you effectively get a zero-height header.
返回 0 会导致 UITableView 使用默认值。这是无证行为。如果你返回一个非常小的数字,你实际上会得到一个零高度的标题。
Swift 3:
斯威夫特 3:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.leastNormalMagnitude
}
return tableView.sectionHeaderHeight
}
Swift:
迅速:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.min
}
return tableView.sectionHeaderHeight
}
Obj-C:
对象-C:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return CGFLOAT_MIN;
return tableView.sectionHeaderHeight;
}
回答by Jakub Truhlá?
If you use tableView
style grouped, tableView
automatically set top and bottom insets. To avoid them and avoid internal insets setting, use delegate methods for header and footer. Never return 0.0 but CGFLOAT_MIN
.
如果您使用tableView
style grouped,则tableView
自动设置顶部和底部插入。为了避免它们并避免内部插入设置,请对页眉和页脚使用委托方法。永远不要返回 0.0 但CGFLOAT_MIN
.
Objective-C
目标-C
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// Removes extra padding in Grouped style
return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// Removes extra padding in Grouped style
return CGFLOAT_MIN;
}
Swift
迅速
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// Removes extra padding in Grouped style
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// Removes extra padding in Grouped style
return CGFloat.leastNormalMagnitude
}
回答by FBente
This worked for me with Swift 4. Modify your UITableView
e.g. in viewDidLoad
:
这对我有用 Swift 4。修改您的UITableView
例如viewDidLoad
:
// Remove space between sections.
tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0
// Remove space at top and bottom of tableView.
tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
tableView.tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
回答by Odd-Arne Dahle
You can try this:
你可以试试这个:
In the loadView
在里面 loadView
_tableView.sectionHeaderHeight = 0;
Then
然后
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0;
}
It should be removed as long as you do not have any objects in the header...
只要标题中没有任何对象,就应该将其删除...
And if you want some size of the sectionheader, then change only the return value.
如果您想要部分标题的大小,则仅更改返回值。
same if you do not get the sectionfooter removed.
如果您没有删除 sectionfooter,则相同。
_tableView.sectionFooterHeight = 0;
and
和
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
Well, this works for my problems with the tableview in the iOS7.
嗯,这适用于我在 iOS7 中的 tableview 的问题。
回答by Chris Yim
You should remove the code self.tableView.tableHeaderView = [UIView new];
after you add
self.tableView.tableHeaderView = [UIView new];
添加后您应该删除代码
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return CGFLOAT_MIN;
}
回答by George Asda
In swift 2.0
在 swift 2.0
func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return yourHeight
}
回答by Divyam shukla
you can use viewForHeaderInSection
and return a view with any height.
您可以使用viewForHeaderInSection
和返回任何高度的视图。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
int height = 30 //you can change the height
if(section==0)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
return view;
}
}
回答by jaiswal Rajan
In Swift 4
在斯威夫特 4
Remove extra top padding in grouped tableview.
删除分组 tableview 中的额外顶部填充。
Here height is given 1 as minimal height for the section header because you cannot give 0 as tableview will take it default top margin if assigned zero height.
这里的高度被指定为 1 作为节标题的最小高度,因为你不能给 0,因为如果指定的高度为零,tableview 将采用默认的顶部边距。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
回答by Jasper
Example of viewForHeaderInSection:
viewForHeaderInSection 示例:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 118)];
view.backgroundColor = COLOR_DEFAULT;
NSString* key = [self.tableKeys objectAtIndex:section];
NSArray *result = (NSArray*)[self.filteredTableData objectForKey:key];
SZTicketsResult *ticketResult = [result objectAtIndex:0];
UIView *smallColoredView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 3)];
smallColoredView.backgroundColor = COLOR_DEFAULT_KOSTKY;
[view addSubview:smallColoredView];
UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 320, 40)];
topBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:topBackgroundView];
UILabel *totalWinnings = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 300, 40)];
totalWinnings.text = ticketResult.message;
totalWinnings.minimumFontSize = 10.0f;
totalWinnings.numberOfLines = 0;
totalWinnings.backgroundColor = [UIColor clearColor];
totalWinnings.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:totalWinnings];
UIView *bottomBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 58)];
bottomBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:bottomBackgroundView];
UILabel *numberOfDraw = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 290, 58)];
numberOfDraw.text = [NSString stringWithFormat:@"sometext %@",[ticketResult.title lowercaseString]];;
numberOfDraw.minimumFontSize = 10.0f;
numberOfDraw.numberOfLines = 0;
numberOfDraw.backgroundColor = [UIColor clearColor];
numberOfDraw.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:numberOfDraw];
return view;