Application effect





Implementation approach

In effect, the image at the bottom of the TableView is pre-loaded, and the advertising cell is transparent, so the basic idea is very clear:

  • Set up an imageView using the tableView’s backgroundView
  • Advertising backgroundColor of the cell and its contentView. BackgroundColor set to clear

Try using native images and implement the following code, which can be intuitively experienced and viewed in SwiftPlayground

import UIKit
import PlaygroundSupport

class ViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        let imageView = UIImageView(image: UIImage(named: "9.jpg"))
        tableView.backgroundView = imageView
        tableView.separatorStyle = .singleLine
    }
}

extension ViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell".for: indexPath)
        cell.backgroundColor = .clear
        cell.contentView.backgroundColor = indexPath.row % 5 == 4 ? .clear : .white        
        return cell
   }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return200}} PlaygroundPage. Current. LiveView = ViewController () / / view in the assistant editorCopy the code

Auxiliary editor

As for the use of Playground here, the execution effect can be viewed in the auxiliary editor without the need for a new project, which is very convenient


PS: Thanks to Wit for sharing this interesting discovery

Add me to wechat to communicate.



Author: Skylink New _ Rigeng

Source: www.jianshu.com/p/0091b46e0…