IT

UITableView의 섹션 사이 간격을 줄입니다.

lottoking 2020. 6. 21. 19:14
반응형

UITableView의 섹션 사이 간격을 줄입니다.


UITableView의 두 섹션 사이의 간격을 줄이는 방법이 있습니까? 내가 가진 모든 단일 섹션 사이에 약 15 픽셀이 있습니다. 이미 0을 반환하려고 않았다 -tableView:heightForFooterInSection:-tableView:heightForHeaderInSection:하지만 아무것도 변경되지 않습니다.

어떤 제안?


조금 까다로 웠지만이 코드를 사용해보십시오.

- (CGFloat)tableView:(UITableView*)tableView 
           heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 6.0;
    }

    return 1.0;
}

- (CGFloat)tableView:(UITableView*)tableView 
           heightForFooterInSection:(NSInteger)section {
    return 5.0;
}

- (UIView*)tableView:(UITableView*)tableView 
           viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView*)tableView:(UITableView*)tableView 
           viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

그에 따라 값을 변경하십시오. 공백을 제거하려면 0.0이 허용되지 않을 것이라고 생각합니다. 가장 작은 제정신 값은 1.0 인 것 같습니다.


거리를 0으로 줄이려면 다음을 사용해야합니다.

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

UITableViewDelegate를 제공하면 0보다 큰 수레에서 시작하는 효과 만 만들기 때문입니다.

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0;
}


-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

(XCode 4.0.2와 함께 iOS 4.1 사용)


실제로 크기 탭 아래의 인터페이스 빌더에서 바닥 글 / 헤더 / 셀 높이를 설정할 수 있습니다. 기본적으로 머리글 / 바닥 글은 10.0으로 설정되어 있습니다.


You have to reduce the section header/footer height. Then the space between sections will be reduce.

try this code

It works for me :-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;

You can also reduce the height of section footer and header from the storyboard. In the tableview -> size inspector. Go to Section Height.

Size inspector for UITableView in storyboard.

By default it is set to 22 for Plain style table and 10 for grouped style table. You can configure values by increasing / decreasing the values for header and footer separately.


For me the issue was because I was using a grouped table view style (UITableViewStyleGrouped). When I changed it to a plain style (UITableViewStylePlain) my tableView:heightForHeaderInSection: method was the only thing determining the size of each section header.


UPDATE FOR iOS7: Due to ARC autorelease update, here's @Martin Stolz code edited.

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 6;
    return 1.0;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 5.0;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

(Using iOS7, Xcode v5.0)


Use this perhaps, 0 will not work as expected

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
     return .leastNormalMagnitude
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}

For me just this issue got resolved just by using the following code,

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? 1 : 20 // 20 is my other sections height
}

Returning 1 for the first section has solved the issue.

참고URL : https://stackoverflow.com/questions/2817308/reducing-the-space-between-sections-of-the-uitableview

반응형