I have been trying to add a scroll view to my table view's cell, but have been running into issues with the images being missing. The pages in the scroll view seem to be loading correctly (based on the debug view hierarchy), but none of the images are showing.
Here is my code:
class AppImagesTableViewCell: UITableViewCell, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var label: UILabel!
var pageViews: [UIImageView?] = []
var sixPlusImages: [UIImage?] = []
override func awakeFromNib() {
super.awakeFromNib()
sixPlusImages = [
UIImage(named: "Glance 5.5.png"),
UIImage(named: "Currencies 5.5.png"),
UIImage(named: "Swipe 5.5.png"),
UIImage(named: "Format 5.5.png"),
UIImage(named: "Search 5.5.png")
]
let pageCount = sixPlusImages.count
pageControl.currentPage = 0
pageControl.numberOfPages = pageCount
for _ in 0..<pageCount {
pageViews.append(nil)
}
let pagesScrollViewSize = scrollView.frame.size
scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(sixPlusImages.count), height: pagesScrollViewSize.height)
loadVisiblePages()
}
func loadPage(page: Int) {
if page < 0 || page >= sixPlusImages.count {
return
}
if let pageView = pageViews[page] {
} else {
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.y = 0.0
let newPageView = UIImageView(image: sixPlusImages[page])
newPageView.contentMode = .ScaleAspectFit
newPageView.frame = frame
scrollView.addSubview(newPageView)
pageViews[page] = newPageView
}
}
func loadVisiblePages() {
let pageWidth = scrollView.frame.size.width
let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
let firstPage = page - 1
let lastPage = page + 1
for var index = 0; index < firstPage; ++index {
purgePage(index)
}
for index in firstPage...lastPage {
loadPage(index)
}
for var index = lastPage+1; index < sixPlusImages.count; ++index {
purgePage(index)
}
pageControl.currentPage = page
}
func scrollViewDidScroll(scrollView: UIScrollView) {
loadVisiblePages()
}
func purgePage(page: Int) {
if page < 0 || page >= sixPlusImages.count {
return
}
if let pageView = pageViews[page] {
pageView.removeFromSuperview()
pageViews[page] = nil
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
And here is what the view hierarchy looks like:
Also, the scroll view allows me to scroll in any direction, rather than just horizontally as it should. I can't see why it does this, as the other scroll view I use, doesn't do this.
Anyone see any issues?