我有一个静态tableView,其中一个单元格是一个带有scrollview和pageControl的自定义单元格,用于显示图像,类似幻灯片,分页。
为此,我创建了自定义表视图单元格,如下所示。我可以在运行模拟器和paginationControl时看到第一个图像,但水平滚动视图不起作用。我找不到让scrollview在其中工作的方法。是因为它是一个静态的细胞吗?或者我错过了什么?
class AdImageViewerTableViewCell: UITableViewCell, UIScrollViewDelegate {
@IBOutlet weak var adImagesScrollView: UIScrollView!
@IBOutlet weak var adImagesPageControl: UIPageControl!
override func awakeFromNib() {
super.awakeFromNib()
self.configureImagePageControl()
self.adImagesScrollView.isPagingEnabled = true
adImagesScrollView.delegate = self
// test purposes
for index in 0...3 {
var frame = CGRect.zero
frame.origin.x = self.adImagesScrollView.frame.size.width * CGFloat(index)
frame.origin.y = 0
frame.size = self.adImagesScrollView.frame.size
let myImageView: UIImageView = UIImageView()
// test
myImageView.sd_setImage(with: URL(string:"https://images-americanas.b2w.io/produtos/01/00/item/123402/7/123402763SZ.jpg"))
myImageView.contentMode = .scaleAspectFit
myImageView.frame = frame
self.adImagesScrollView.addSubview(myImageView)
self.addSubview(self.adImagesPageControl)
}
self.adImagesScrollView.contentSize = CGSize(width: self.adImagesScrollView.frame.size.width, height: self.adImagesScrollView.frame.size.height)
self.adImagesPageControl.addTarget(self, action: Selector(("changePage:")), for: UIControlEvents.valueChanged)
}
func configureImagePageControl() {
// The total number of pages that are available is based on how many available colors we have.
self.adImagesPageControl.layer.zPosition = 1
self.adImagesPageControl.numberOfPages = 4
//self.adImagesPageControl.numberOfPages = (ad.adImages?.count)!
self.adImagesPageControl.currentPage = 0
self.adImagesPageControl.tintColor = UIColor.red
self.adImagesPageControl.pageIndicatorTintColor = UIColor.black
self.adImagesPageControl.currentPageIndicatorTintColor = UIColor.green
self.addSubview(adImagesPageControl)
}
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:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1
self.adImagesPageControl.currentPage = Int(currentPage)
}
func changePage(sender: AnyObject) -> () {
let x = CGFloat(adImagesPageControl.currentPage) * adImagesScrollView.frame.size.width
adImagesScrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}