关于swift3:在静态单元格UITableView上添加背景图片

add background image on Static Cell UITableView

有人知道如何在静态UITableView上放置背景图像吗?我有一个动态原型UITableView和一个静态UITableView。(两个tableView都显示很多行)而且我想在两个tableview上使用相同的背景图像。但是我不确定如何在静态单元格UITableView上添加背景图像。有什么主意吗?


希望这会有所帮助。

您可以将此代码添加到viewdidload

1
2
3
   let TempImageView = UIImageView(image: UIImage(named:"your_image"))
   TempImageView.frame = self.TableView.frame
   self.TableView.backgroundView = TempImageView

您将需要创建tableview类,并且必须添加以下方法-

1
2
3
 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return super.tableView .numberOfRows(inSection: section)
    }

还要确保将单元格背景设置为清除颜色

1
2
3
4
5
6
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = super.tableView(tableView, cellForRowAt: indexPath)
        cell.backgroundColor = UIColor.clear
        return cell
    }