如何在 TableViewCell 中使用自定义 ImageView ( class )

how to use a Custom ImageView ( class ) in a TableViewCell

我正在尝试在我的 UITableViewCell 中获取它

一个 imageView,它可以容纳一个图像数组并且可以滚动

所以我尝试了这个:

class MostWantedImageView: UIImageView ,  UIScrollViewDelegate{



var pageImages:[UIImage] = [UIImage]()
var pageViews:[UIImageView?] = [UIImageView]()

var scrollView:UIScrollView = UIScrollView()
var pageControl:UIPageControl = UIPageControl()

var viewingPage = -1

func loadVisiblePages() {
    // First, determine which page is currently visible
    let pageWidth:CGFloat = self.frame.size.width;
    let page = Int(floor((self.scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)));

    /*
     Check that page have changed,
     in case that user drag left in first page, or drag right in last page
     a 'scrollViewDidEndDecelerating' is fired
     */
    if viewingPage != page {

        // Update the page control
        self.pageControl.currentPage = page;

        // Work out which pages we want to load
        let firstPage = page - 1;
        let lastPage = page + 1;

        // Purge anything before the first page
        for (var i=0; i<firstPage; i += 1) {
            self.purgePage(i)
        }
        for (var i=firstPage; i<=lastPage; i += 1) {
            self.loadPage(i)
        }
        for (var i = lastPage+1 ; i < self.pageImages.count ; i += 1) {
            self.purgePage(i)
        }

        viewingPage = page
    }

}

func loadPage(page:Int) {
    if page < 0 || page >= self.pageImages.count {
        // If it's outside the range of what we have to display, then do nothing
        return;
    }

    // Load an individual page, first seeing if we've already loaded it
    let pageView:UIImageView? = self.pageViews[page];
    if pageView == nil {
        var frame:CGRect = self.bounds;
        //frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.x = 320.0 * CGFloat(page)
        frame.origin.y = 0.0

        var newPageView:UIImageView = UIImageView(image: self.pageImages[page])
        newPageView.contentMode = UIViewContentMode.ScaleAspectFit;
        newPageView.frame = frame;
        self.scrollView.addSubview(newPageView)
        self.pageViews[page] = newPageView
    }
}

func purgePage(page:Int) {
    if page < 0 || page >= self.pageImages.count {
        // If it's outside the range of what we have to display, then do nothing
        return;
    }

    // Remove a page from the scroll view and reset the container array
    let pageView:UIView? = self.pageViews[page];
    if pageView != nil {
        pageView?.removeFromSuperview()
        self.pageViews[page] = nil
    }
}



    override init(frame: CGRect ) {
        super.init(frame: frame )


    self.backgroundColor = UIColor.blueColor()

    // Set up the image we want to scroll & zoom and add it to the scroll view

    self.pageImages.append(UIImage(named: "event1")!)
    self.pageImages.append(UIImage(named: "event2")!)
    self.pageImages.append(UIImage(named: "event3")!)

    let pageCount = self.pageImages.count

    self.scrollView.pagingEnabled = true
    self.scrollView.delegate = self
    self.scrollView.showsHorizontalScrollIndicator = false
    self.scrollView.showsVerticalScrollIndicator = false

    // Set up the page control
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;

    self.pageControl.frame = self.frame
    self.scrollView.frame = self.frame

    self.pageControl.translatesAutoresizingMaskIntoConstraints = false
    self.scrollView.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(self.pageControl)
    self.addSubview(self.scrollView)
    self.userInteractionEnabled = true


    //Set layout
    var viewsDict = Dictionary <String, UIView>()
    viewsDict["control"] = self.pageControl;
    viewsDict["scrollView"] = self.scrollView;

    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[control]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView(400)]-[control]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))

    // Set up the array to hold the views for each page
    for (var i = 0; i < pageCount; ++i) {
        self.pageViews.append(nil)
    }


    // Set up the content size of the scroll view
    let pagesScrollViewSize:CGSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.pageImages.count), pagesScrollViewSize.height);

    // Load the initial set of pages that are on screen
    self.loadVisiblePages()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

init() {


    super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}




func scrollViewDidEndDecelerating(scrollView: UIScrollView!) {
    self.loadVisiblePages()
}


func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / image.size.width
    let heightRatio = targetSize.height / image.size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
    } else {
        newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRectMake(0, 0, newSize.width, newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    //UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    return newImage
}


}

它是我放置 scrollView 和 UIPageControll 的 ImageView 我可以在我的 ViewController 中使用它,但不知道如何在我的 TableViewCell 中使用它,如果有人可以给我一些指导,那么它会对我有帮助

请改用 CollectionView。随着图像数量的增加,您的应用程序将出现性能问题。

你需要看看这个- putting-a-uicollectionview-in-a-uitableviewcell-in-swift

这将帮助您了解如何在 table 单元格中使用 collectionView。