在 Static Cell UITableView 上添加背景图片

add background image on Static Cell UITableView

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

希望对您有所帮助。

您可以将此代码添加到 viewdidload

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

您将需要创建表视图 class 并且您必须添加如下方法 -

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

还要确保将单元格背景设置为透明色

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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