未在UITableViewCell Swift中加载的图像

时间:2015-11-25 16:23:01

标签: ios swift scrollview tableviewcell

  1. 这是对TableView中的每个Cell重复的函数。正如您所看到的,我在名为Product的对象中传递了一组图像(称为imageset)。 productImages是一个Product数组。在这个对象里面我们有Name,Price和imageSet。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("productCell", forIndexPath: indexPath) as! ProductHomeCell
    
        cell.productName.text = productImages[indexPath.row].name
        cell.productPrice.text = "\(productImages[indexPath.row].price)"
    
        //cell.PageImages contains Array of Images
        cell.pageImages = productImages[indexPath.row].imageSet
    
        return cell
    }
    
  2. 现在我们将查看Custom UITableViewCell,其中将加载和显示所看到的Product的信息。注意我使用ScrollView滚动图像。(我从stackoverflow中的问题中获取代码) PageImages是应该传递imageSet的变量。

    var pageImages: [UIImage] = []
    
    var pageViews: [UIImageView?] = []
    var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(60, 300, 195, 5))
    
    
    override func awakeFromNib() {
    
    
        super.awakeFromNib()
        scrollView.delegate = self
        pageControl = UIPageControl(frame: CGRectMake(230, 14, 107, 37))
    
        //PageImages should be loaded.
        let pageCount = pageImages.count
        self.pageControl.numberOfPages = pageCount
        configurePageControl()
        pageControl.enabled = false
    
        // 3
        for _ in 0..<pageCount {
            pageViews.append(nil)
        }
    
        // 4
        let pagesScrollViewSize = scrollView.frame.size
    
    
        scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
            height: pagesScrollViewSize.height)
    
    
        loadVisiblePages()
    
    }
    
    func loadPage(page: Int) {
        if page < 0 || page >= pageImages.count {
            // If it's outside the range of what you have to display, then do nothing
            return
        }
    
        // 1
        if let pageView = pageViews[page] {
            // Do nothing. The view is already loaded.
        } else {
            // 2
            var frame = scrollView.bounds
            frame.origin.x = frame.size.width * CGFloat(page)
            frame.origin.y = 0.0
    
            // 3
            let newPageView = UIImageView(image: pageImages[page])
            // let newPageView = UIImageView(image: imageC)
    
            newPageView.contentMode = .ScaleAspectFit
            newPageView.frame = frame
            scrollView.addSubview(newPageView)
    
            // 4
            pageViews[page] = newPageView
        }
    }
    
    func loadVisiblePages() {
        // First, determine which page is currently visible
        let pageWidth = scrollView.frame.size.width
        let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
    
        // Work out which pages you want to load
        let firstPage = page - 1
        let lastPage = page + 1
    
        // Purge anything before the first page
        for var index = 0; index < firstPage; ++index {
            purgePage(index)
        }
    
        // Load pages in our range
        for index in firstPage...lastPage {
            loadPage(index)
        }
    
        // Purge anything after the last page
        for var index = lastPage+1; index < pageImages.count; ++index {
            purgePage(index)
        }
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // Load the pages that are now on screen
        loadVisiblePages()
    }
    
    func purgePage(page: Int) {
        if page < 0 || page >= pageImages.count{
            // If it's outside the range of what you have to display, then do         nothing
            return
        }
    
        // Remove a page from the scroll view and reset the container array
        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
    }
    
    
    func configurePageControl() {
    
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.blackColor()
    
        self.pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
        self.pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
        self.addSubview(pageControl)
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = Int(pageNumber)
    }
    
  3. 问题 - 产品名称和价格完美加载。图像不。当我调试时,当awakeFromNib()正在运行时看起来像PageImages是空的,但是当我在cellForARowAtIndexPath方法中调试时,var PageImages会在该indexPath.row处加载imageSet。 感谢

1 个答案:

答案 0 :(得分:0)

awakeFromNib返回单元格之前执行

dequeue...因此,尝试设置您的网页无法正常工作。我所见过的两种方法是要么有一种方法来明确设置单元格:

func setupCell(productName:String, productPrice:Double, images:[UIImage]) {
    // Do most of what you have in awakeFromNib here
}

或者在属性的didSet中执行:

var pageImages : [UIImage] = [] {
    didSet {
        // Set up your page controller here.
    }
}