我试图通过代码在部分中设置页眉和页脚的高度,但它不起作用

问题描述 投票:0回答:1

我尝试通过代码设置表视图中部分的页脚和页眉高度,但它不起作用。当我从故事板中执行此操作时,效果很好。

这是我的代码



import UIKit

class MyTableViewController: UIViewController {

    @IBOutlet weak var myTable : UITableView!
    
    let spacing : CGFloat = 20
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myTable.delegate = self
        myTable.dataSource = self
    }
    

}

extension MyTableViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTable.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "Random text"
        cell.accessoryType = .detailButton
        return cell
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
       
        return spacing
        
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return spacing
    }
    
    
}


代码的输出;

我想给单元格添加一些填充

swift xcode uitableview uikit
1个回答
0
投票

默认情况下,如果您不符合,

UITableViewDelegate
将在
viewForHeaderInSection
中返回nil。添加此委托方法:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    //... some configuration here
    return view
}
© www.soinside.com 2019 - 2024. All rights reserved.