Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save YogeshPateliOS/b2b13bfe5f7eef5cd7fa4a894cd35d5a to your computer and use it in GitHub Desktop.

Select an option

Save YogeshPateliOS/b2b13bfe5f7eef5cd7fa4a894cd35d5a to your computer and use it in GitHub Desktop.

Revisions

  1. YogeshPateliOS created this gist Jun 1, 2020.
    92 changes: 92 additions & 0 deletions UITableView And Diffable Data Source
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@

    import UIKit

    @available(iOS 13.0, *)
    typealias UserDataSource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>
    @available(iOS 13.0, *)
    typealias UserSnapshot = NSDiffableDataSourceSnapshot<TblSection, YoutubeVideoModel>

    enum TblSection{
    case first
    }

    class SearchViewController: UIViewController {

    @IBOutlet weak var lblRecord: UILabel!
    @IBOutlet weak var searchTblView: UITableView!

    var arrVideo = [YoutubeVideoModel]()

    @available(iOS 13.0, *)
    lazy var datasource = UserDataSource()

    override func viewDidLoad() {
    super.viewDidLoad()
    lblRecord.text = "Let's start searching videos"
    if #available(iOS 13.0, *) {
    configureDatasource()
    } else {
    // Fallback on earlier versions
    }
    searchTblView.register(VideoTableViewCell.self, forCellReuseIdentifier: "VideoTableViewCell")
    setupSearchController()
    APICalling()
    }

    @available(iOS 13.0, *)
    func getDatasource() -> UserDataSource{
    var datasource: UserDataSource!
    return datasource
    }

    @available(iOS 13.0, *)
    func configureDatasource(){
    datasource = UITableViewDiffableDataSource<TblSection, YoutubeVideoModel>(tableView: searchTblView, cellProvider: { (tableView, indexPath, modelVideo) -> VideoTableViewCell? in
    self.configurationCell(indexPath: indexPath)
    })
    }

    @available(iOS 13.0, *)
    func createSnapshot(users: [YoutubeVideoModel]){
    var snapshot = UserSnapshot()
    snapshot.appendSections([.first])
    snapshot.appendItems(users)
    datasource.apply(snapshot, animatingDifferences: true)
    }

    func configurationCell(indexPath: IndexPath) -> VideoTableViewCell{
    let cellVideo = VideoTableViewCell.cellVideo
    var cell = searchTblView.dequeueReusableCell(withIdentifier: cellVideo.identifier, for: indexPath) as? VideoTableViewCell
    if cell != nil{
    if let arr = Bundle.main.loadNibNamed("VideoTableViewCell", owner: self, options: [:]){
    cell = arr[cellVideo.index] as? VideoTableViewCell
    }
    }
    let modelVideo = arrVideo[indexPath.row]
    cell?.modelVideo = modelVideo
    cell?.tapYoutube = {
    modelVideo.videoId.playInYoutube()
    }
    cell?.tapWatchLater = {
    DatabaseHelper.shareInstance.saveYoutubeVideo(modelYoutube: modelVideo)
    }
    return cell!
    }

    }



    extension SearchViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    arrVideo.count
    }

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