简体   繁体   English

具有自定义单元格的TableView重复第一个和最后一个单元格包含图像

[英]TableView with custom cells repeats first and last cell's containing images

Im having trouble when displaying images inside a ScrollView in a custom cell class called ProductHomeCell . 在名为ProductHomeCell的自定义单元类中,在ScrollView中显示图像时遇到问题。 The images contained in the last cell's horizontal scrollView are copied in the first cell's when I go scrolling down. 当我向下滚动时,最后一个单元格的水平scrollView中包含的图像被复制到第一个单元格中。 Heres the method that fills each custom cell. 下面是填充每个自定义单元格的方法。

 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.matchLbl.text = "\(productImages[indexPath.row].match)"

        // cell.PageImages contains Array of Images for scrollView
        cell.pageImages = productImages[indexPath.row].imageSet

    return cell

}

Heres some attributes and methods from ProductHomeCell that sets the scrollView with the images sent and the other attributes. 下面是ProductHomeCell的一些属性和方法,它们使用发送的图像和其他属性设置scrollView。

 import Foundation
 import UIKit


 class ProductHomeCell: UITableViewCell, UIScrollViewDelegate {


@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var matchLabel: UILabel!

@IBOutlet weak var matchLbl: UILabel!


@IBOutlet weak var productName: UILabel!

@IBOutlet weak var comprarBtn: UIButton!

@IBOutlet weak var productPrice: UILabel!



var pageViews: [UIImageView?] = []
var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(60, 300, 200, 5))

var pageImages : [UIImage] = [] {

    didSet {

        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)

        if pageCount == 1 {

            self.pageControl.hidden = true

        }

        loadVisiblePages()





    }


    }


override func awakeFromNib() {



    super.awakeFromNib()
    scrollView.delegate = self
    pageControl = UIPageControl(frame: CGRectMake(240, 0, 107, 37))
    likeButton.setImage(UIImage(named:"corazon_borde_rojo"),forState:UIControlState.Normal)

    comprarBtn.layer.borderColor = UIColor.lightGrayColor().CGColor
    comprarBtn.layer.borderWidth = 0.5
    comprarBtn.layer.cornerRadius = 12.0
    comprarBtn.clipsToBounds = true

    // 4
    let scrollViewFrame = scrollView.frame
    let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
    let minScale = min(scaleWidth, scaleHeight);
    scrollView.minimumZoomScale = minScale;

    // 5
    scrollView.maximumZoomScale = 1.0
    scrollView.zoomScale = minScale;




}

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)
}

@IBOutlet weak var likeButton: UIButton!


@IBAction func darLike(sender: AnyObject) {

    if((likeButton.imageView?.image?.isEqual(UIImage(named: "corazon_borde_rojo"))) != nil){
        likeButton.setImage(UIImage(named:"corazon_rojo"),forState: UIControlState.Normal)



    }

}

@IBAction func verDetail(sender: AnyObject) {



}


override func prepareForReuse() {
    super.prepareForReuse()

    loadVisiblePages()

}





 }

This class also contains methods that set up the scroll view and pageControl horizontally with the correspondent images. 此类还包含使用对应图像水平设置滚动视图和pageControl的方法。

The PROBLEM as I mentioned is that when I scroll down the images from the last cells replace the first cell's images but not the attributes as price or description. 我提到的问题是,当我向下滚动最后一个单元格中的图像时,会替换第一个单元格的图像,而不是作为价格或描述的属性。 ONLY images. 只有图像。 Thanks! 谢谢!

您的单元格将被重用,因此请确保在设置单元格之前清除所有值。

问题是我必须清空scrollView,删除cellForRowAtIndexPath方法中的每个subView及其所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM