简体   繁体   English

表格视图单元格中的滚动视图将无法正确显示图像,并且在Swift 4的分页模式下会有一页额外的页面

[英]scroll view in table view cell won't show correctly images and there is one extra page in paging mode in swift 4

I used scroll view with images in table view cell But there are two problems : 1- there is an extra page not the count of my images and 2: the image doesn't fill the screen and there are white space between each image 我在表格视图单元格中使用了带有图像的滚动视图,但是有两个问题:1-有一个额外的页面而不是我的图像计数;并且2:该图像无法填充屏幕,并且每个图像之间都有空白 这是我所看到的图像

and here is my table view cell with scroll view codes 这是我的带有滚动视图代码的表格视图单元格

class scrollViewCell: UITableViewCell , UIScrollViewDelegate {

@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!

var contentWidth : CGFloat = 0.0


override func awakeFromNib() {
    super.awakeFromNib()        

    scrollView.delegate = self
    for image in 0...2 {
        let imageToDisplay = UIImage(named : "1")
        let imageView = UIImageView(image : imageToDisplay)

        let xCoordinate = self.contentView.frame.midX + self.contentView.frame.width * CGFloat(image)
        contentWidth += self.contentView.frame.width
        scrollView.addSubview(imageView)

        imageView.frame = CGRect(x: xCoordinate - 50 , y: 0 , width: self.contentView.frame.width, height: 100)

        imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
        imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
        imageView.clipsToBounds = true


    }

    scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)


}


func scrollViewDidScroll(_ scrollView: UIScrollView) {
    pageControl.currentPage = Int(scrollView.contentOffset.x / CGFloat(414))
}


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

    // Configure the view for the selected state
}
}

Can you try this 你可以试试这个吗

func addscrollV()
{
     for image in 0...2 {
    let imageToDisplay = UIImage(named : "1")
    let imageView = UIImageView(image : imageToDisplay)

    let xCoordinate = self.contentView.frame.width * CGFloat(image)
    contentWidth += self.contentView.frame.width
    scrollView.addSubview(imageView)

    imageView.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)

    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
   }

   scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)

}

func layoutSubviews()
{
  super.layoutSubviews()
   if(Once)
   {
     Once = false
     self.addscrollV()
  }
}

Issues is in your xCoordinate calculation, the contentView frame wont be available in awakeFromNib. 问题出在您的xCoordinate计算中,contentView框架在aakeakeFromNib中将不可用。

You should not user super.awakeFromNib to calculate the frames. 您不应使用super.awakeFromNib来计算帧。 create a method called 创建一个称为

func configureUI(){ //Update your UI here }

Call this method from cellForRowAtIndex: delegate method of UITableViewCell cellForRowAtIndex:调用此方法cellForRowAtIndex: UITableViewCell的委托方法

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

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