Skip to content

Instantly share code, notes, and snippets.

@MaxHasADHD
Last active September 2, 2015 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxHasADHD/ece797ebe14d777b27f4 to your computer and use it in GitHub Desktop.
Save MaxHasADHD/ece797ebe14d777b27f4 to your computer and use it in GitHub Desktop.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PopularShowCell", forIndexPath: indexPath) as! PopularShowTableViewCell
// Configure the cell...
configureCell(cell, indexPath: indexPath)
return cell
}
func configureCell(cell: PopularShowTableViewCell, indexPath: NSIndexPath) {
if segmentedControl.selectedSegmentIndex == 0 {
let show = popularShowsResultsController.objectAtIndexPath(indexPath) as! TVPopularShow
cell.titleLabel?.text = show.title
cell.delegate = self
let traktID = show.idTrakt
let showAlreadySaved = showIDs.indexOf(traktID.integerValue) != nil
let showDownloading = showsDownloading.indexOf(traktID.integerValue) != nil
let isWatching = showAlreadySaved || showDownloading
cell.isWatching = isWatching
// Accessibility
cell.accessibilityLabel = "\(show.title)"
cell.accessibilityValue = isWatching ? NSLocalizedString("ACCESSIBILITY_VALUE_TRACKING", comment: "Tracking") : NSLocalizedString("ACCESSIBILITY_VALUE_NOT_TRACKING", comment: "Not tracking")
cell.accessibilityHint = NSLocalizedString("ACCESSIBILITY_HINT_SHOW_INFORMATION", comment: "Double tap to view show information")
// Image
ImageManager.sharedManager.getImageForShow(show, completionHandler: { (image) -> Void in
if self.popularIndexPathsForFadedInImages.indexOf(indexPath) == nil {
self.popularIndexPathsForFadedInImages.append(indexPath)
if let fetchCell = self.tableView.cellForRowAtIndexPath(indexPath) as? PopularShowTableViewCell {
func fadeInImage() {
// Fade in image
fetchCell.backgroundImageView!.alpha = 0.0
fetchCell.backgroundImage = image
UIView.animateWithDuration(showImageAnimationSpeed, animations: { () -> Void in
fetchCell.backgroundImageView!.alpha = 1.0
})
}
if #available(iOS 9.0, *) {
if NSProcessInfo.processInfo().lowPowerModeEnabled {
fetchCell.backgroundImage = image
}
else {
fadeInImage()
}
}
else {
fadeInImage()
}
}
else {
// THIS IS THE ISSUE.
// indexPathForCell will return nill, cellForRowAtIndexPath is nil, and if I set the image
// on the cell directly (look at the else statement directly below)
// it will make the wrong cells load the wrong images when scrolling fast and all
print("Cant find cell at index path: \(indexPath)")
print(self.tableView.numberOfRowsInSection(0))
print(NSThread.currentThread())
}
}
else {
// Set image
cell.backgroundImage = image
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment