Juanpe / SkeletonView

☠️ An elegant way to show users that something is happening and also prepare them to which contents they are awaiting
MIT License
12.53k stars 1.1k forks source link

Not working shimmer #284

Open mohsinalimat opened 4 years ago

mohsinalimat commented 4 years ago

hello,

Thanks, Mohsinali Matiya

mohsinalimat commented 4 years ago

self.view.isSkeletonable = true

When I used above line with below line then work fine.

self.view.showAnimatedGradientSkeleton()

Mukul13cs002 commented 4 years ago

`struct Photo: Codable { let albumID, id: Int let title, url, thumbnailURL : String

enum CodingKeys: String, CodingKey {
    case albumID = "albumId"
    case thumbnailURL = "thumbnailUrl"
    case id, title, url
}

}

class PhotosController: UITableViewController {

private var dataSource: [Photo] = []

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(PhotoCell.self, forCellReuseIdentifier: "cell")
    tableView.separatorStyle = .none
    tableView.estimatedRowHeight = 120.0
    view.isSkeletonable = true
    tableView.isSkeletonable = true
    guard let url = URL(string: "https://jsonplaceholder.typicode.com/photos") else {
        return
    }
    let request = URLRequest(url: url)
    view.showAnimatedSkeleton()
    URLSession.shared.dataTask(with: request) { data, response, error in
               if let data = data {
                   if let decodedResponse = try? JSONDecoder().decode([Photo].self, from: data) {
                       DispatchQueue.main.async {
                           self.dataSource = decodedResponse
                            self.view.hideSkeleton()
                            self.tableView.reloadData()
                       }
                   }
               }
           }.resume()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataSource.count
}

}

extension PhotosController : SkeletonTableViewDataSource {

func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}

func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
    return "cell"
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PhotoCell
    cell.textLabel?.text = dataSource[indexPath.row].title
    cell.detailTextLabel?.text = dataSource[indexPath.row].url
    return cell
  }

}

class PhotoCell: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
    isSkeletonable = true
    textLabel?.isSkeletonable = true
    textLabel?.numberOfLines = 3
    detailTextLabel?.isSkeletonable = true
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

} ` I am trying to achieve this from programmatically but it is not working. please help me.