테이블보기에서 섹션 머리글의 높이를 변경하는 방법을 알고 있습니다. 그러나 첫 번째 섹션 전에 기본 간격을 변경하는 해결책을 찾을 수 없습니다.
지금이 코드가 있습니다.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0){
return 0;
}
return 10;
}

테이블보기에서 섹션 머리글의 높이를 변경하는 방법을 알고 있습니다. 그러나 첫 번째 섹션 전에 기본 간격을 변경하는 해결책을 찾을 수 없습니다.
지금이 코드가 있습니다.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0){
return 0;
}
return 10;
}

답변:
CGFLOAT_MIN원하는 섹션 높이로 0 대신 반환 하십시오.
0을 반환하면 UITableView가 기본값을 사용합니다. 이것은 문서화되지 않은 동작입니다. 아주 작은 숫자를 반환하면 높이가 0 인 헤더를 효과적으로 얻을 수 있습니다.
스위프트 3 :
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.leastNormalMagnitude
}
return tableView.sectionHeaderHeight
}
빠른:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.min
}
return tableView.sectionHeaderHeight
}
Obj-C :
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return CGFLOAT_MIN;
return tableView.sectionHeaderHeight;
}
CGFloat.leastNormalMagnitude
estimatedHeightForHeaderInSection. 앱이 충돌합니다.
당신이 사용하는 경우 tableView스타일 그룹화 , tableView자동으로 설정 상단과 하단 세트를. 이를 방지하고 내부 삽입 설정을 방지하려면 머리글 및 바닥 글에 대리자 메서드를 사용하십시오. 0.0을 반환하지 않고CGFLOAT_MIN .
- (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;
}
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
}
viewForHeaderInSection완전히 사라질 때까지 헤더.
높이가 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];
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1.0f; }
이것은 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)))
이것을 시도 할 수 있습니다.
에서 loadView
_tableView.sectionHeaderHeight = 0;
그때
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0;
}
헤더에 개체가없는 한 제거해야합니다.
섹션 헤더의 크기를 원하는 경우 반환 값만 변경하십시오.
sectionfooter가 제거되지 않은 경우에도 동일합니다.
_tableView.sectionFooterHeight = 0;
과
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
글쎄, 이것은 iOS7의 tableview에 대한 내 문제에 대해 작동합니다.
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;
}
}
Swift 4에서
그룹화 된 테이블보기에서 추가 상단 패딩을 제거합니다.
여기서 높이는 0을 지정하면 tableview가 기본 상단 여백을 사용하므로 0을 제공 할 수 없기 때문에 섹션 헤더의 최소 높이로 1이 제공됩니다.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
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;