iPhone UITableView image background when table is empty

前端 未结 3 416
长情又很酷
长情又很酷 2021-02-04 18:17

I would like to show an image background when my UITableView is empty. Currently I tried adding a UIImageView to my view controller that contains a table, but XCode doesn\'t all

3条回答
  •  深忆病人
    2021-02-04 18:50

    In you numberOfRowsInSection method:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        if data.count > 0 {
    
            return data.count
        }
        else{
    
            let image = UIImage(named: "Nature")
            let noDataImage = UIImageView(image: image)
            noDataImage.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.frame.height)
            tableView.backgroundView = noDataImage
            tableView.separatorStyle = .none
    
            return 0
        }
    }
    

    numberOfSections should be greater than 0

提交回复
热议问题