Space Between Sections in UITableview

前端 未结 11 2106
醉酒成梦
醉酒成梦 2020-12-08 12:37

I need to reduce the space between two sections ofUITableView. I looked at this question but the solution doesn\'t allow for my custom header view because it c

相关标签:
11条回答
  • 2020-12-08 13:15

    You can do it by implement the delegate heightForHeaderInSection & heightForFooterInSection.

    The return vaule should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it need a very small value, try CGFLOAT_MIN.

    for my example:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == [self.dataArray indexOfObject:self.bannerList]) {
        return 46;
    }
    return CGFLOAT_MIN;
    

    }

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return CGFLOAT_MIN;
    } 
    
    0 讨论(0)
  • 2020-12-08 13:20

    Along with the answer posted by Icaro I would like to add that you also need to implement the tableView:viewForFooterInSection: method returning nil for the section you want to remove the empty space below It will then become:

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return 0.001f;
    }
    
    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return nil;
    }
    
    0 讨论(0)
  • 2020-12-08 13:23

    Did you try override this function:

    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return .leastNormalMagnitude
    }
    
    0 讨论(0)
  • 2020-12-08 13:26

    Select the tableView in your storyboard/objectCode and ensure that the style is set to "Plain", instead of "Grouped". You can find this setting in the attributes Inspector tab.

    let myTableView : UITableView = {
            let tableView = UITableView(frame: .zero, style: .plain)
            tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
            tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
            tableView.separatorStyle = .none
            tableView.translatesAutoresizingMaskIntoConstraints = false
            return tableView
        }()
    
    0 讨论(0)
  • 2020-12-08 13:28

    You need to use method heightForHeaderInSection for defining space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN which is 0.000001f. Giving you an example, how you can use different section with different header heights

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 0 || section == 2)
        {
            return 55.0;
        }
        else
        {
            return CGFLOAT_MIN;
        }
    }
    

    Hope it will help you.

    0 讨论(0)
  • 2020-12-08 13:31

    For Swift 5+:

    There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.

    My solution to have a separation between 2 sections is the following:

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
            if section == 0 {
                 return 24
            } else if section == 1 {
                 return 32
            }
        }
    
        override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            nil
        }
    
        override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            nil
        }
    
        override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            CGFloat.leastNormalMagnitude
        }
    

    As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.

    In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.

    0 讨论(0)
提交回复
热议问题