raginggoat
raginggoat

Reputation: 3600

UITableViewCell Scroll View

I have a table view cell with a scroll view for scrolling through images. There are a couple of issues I'm having with it.

When the table view is shown, the cell with the scroll view and images shows like the first image and part of the second image and the paging doesn't work and it lets you scroll in any direction.

enter image description here

Once I scroll past this cell and then scroll back to it, it is displayed correctly and paging works like it should.

enter image description here

In cellForRowAtIndexPath()

if indexPath.section == 0 {
            let imageCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! ImageCell

            imageCell.selectionStyle = .None

            if imageFiles.count > 0 {

                if imageFiles.count == 1 {
                    imageCell.pageControl.hidden = true
                }

                imageCell.pageControl.numberOfPages = imageFiles.count
                imageCell.pageControl.currentPage = 0

                var index = 0
                for photo in imageFiles {
                    var frame = CGRect()
                    frame.origin.x = (imageCell.imageScrollView.frame.size.width * CGFloat(index))
                    frame.origin.y = 0
                    frame.size = CGSizeMake(imageCell.imageScrollView.frame.size.width, imageCell.imageScrollView.frame.size.height)

                    let imageView = UIImageView(frame: frame)
                    imageView.image = photo

                    imageCell.imageScrollView.addSubview(imageView)
                    imageCell.imageScrollView.contentSize = CGSizeMake(imageCell.imageScrollView.frame.size.width * CGFloat(imageFiles.count), imageCell.imageScrollView.frame.size.height)

                    index += 1
                }

            } else {
                imageCell.pageControl.hidden = true
                let imageView = UIImageView(frame: imageCell.imageScrollView.frame)
                imageView.image = UIImage(named: "placeholder")
                imageCell.imageScrollView.addSubview(imageView)
            }

My custom cell:

import UIKit

class ImageCell: UITableViewCell, UIScrollViewDelegate {
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var imageScrollView: UIScrollView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let pageWidth =  scrollView.frame.size.width

        pageControl.currentPage = Int(scrollView.contentOffset.x / pageWidth)
    }
}

Upvotes: 1

Views: 569

Answers (2)

raginggoat
raginggoat

Reputation: 3600

I was using SDWebImageDownloader. viewDidLoad() to download images to use in the scroll view but I wasn't reloading the table view data. Once I added that, this works perfectly.

func downloadImages() {
        let downloader = SDWebImageDownloader.sharedDownloader()

        for imageURL in images {
            let url = NSURL(string: imageURL)
            downloader.downloadImageWithURL(url, options: SDWebImageDownloaderOptions.UseNSURLCache, progress: nil,
                                            completed: { (image, data, error, bool) -> Void in
                                                self.imageFiles.append(image)
                                                dispatch_async(dispatch_get_main_queue(), {
                                                    self.tableView.reloadData()
                                                })
            })
        }
    }

Upvotes: 1

Jordan
Jordan

Reputation: 4212

You are doing this:

frame.size = CGSizeMake(imageCell.imageScrollView.frame.size.width, imageCell.imageScrollView.frame.size.height)

I would guess that when your view initially loads, imageScrollView has not been fully laid out, so it doesn't have a valid size. Scrolling away from this cell & back to it after the view is fully loaded would result in the scroll view having a valid size.

You should validate that imageScrollView has the width you think it does, and if it doesn't, maybe use the width of the screen instead.

I have implemented a similar thing in one of my apps, so as another possible option, instead of putting a scroll view in your first cell, you could place a CollectionView in there instead and populate your collection view with cells of images. This would A) allow your layout to dynamically change with the view, and B) rather than loading up your scroll view with all the images, they can be dynamically loaded as needed.

Upvotes: 0

Related Questions