How to remove extra empty cells in TableViewController, iOS - Swift

后端 未结 13 934
暗喜
暗喜 2020-12-22 16:12

Hi I have a TableViewController with two static cells, however when displayed, it shows the two static cells, and then all the other empty cells for the tablevi

相关标签:
13条回答
  • 2020-12-22 16:13

    Setting the tableFooterView should remove extra cell like views.

       @IBOutlet weak var tableView: UITableView! {
            didSet {
                tableView.tableFooterView = UIView(frame: .zero)
            }
        }
    
    0 讨论(0)
  • 2020-12-22 16:15

    Swift 4 Elaborating on HorseT's answer, first control drag from your Table View in your storyboard, to your View Controller class and create an outlet called tableView.

    @IBOutlet weak var tableView: UITableView!
    

    Then, your viewDidLoad in your View Controller should look something like this:

        override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.dataSource = self
        tableView.delegate = self
    
        tableView.tableFooterView = UIView()
    }
    
    0 讨论(0)
  • 2020-12-22 16:15

    Add Below Code in your "override func viewDidLoad()" Or "override func viewWillAppear(_ animated: Bool)" functions.

    tblOutletName.tableFooterView = UIView()

    0 讨论(0)
  • 2020-12-22 16:16

    Based on above answers in swift.

    tableView.tableFooterView = UIView(frame: .zero)
    
    0 讨论(0)
  • 2020-12-22 16:18

    To add to this, the reason why setting tableFooterView to UIView() removes the rows is because you are setting an empty UIView() object to the property. As per the Apple documentation:

    var tableFooterView: UIView?
    

    Therefore setting an empty UIView() will display nothing below your data for this property.

    You can bring the empty rows back by resetting tableFooterView to the default value, like so:

    tableView.tableFooterView = nil
    
    0 讨论(0)
  • 2020-12-22 16:23

    if you want to remove rows for all UITableViews in your app, just add an extension like this:

    Swift 5.0

    extension UITableView {
    
    override open func didMoveToSuperview() {
        super.didMoveToSuperview()
        self.tableFooterView = UIView()
    
    }
    
    0 讨论(0)
提交回复
热议问题