簡體   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

我在表格視圖單元格中使用了帶有圖像的滾動視圖,但是有兩個問題:1-有一個額外的頁面而不是我的圖像計數;並且2:該圖像無法填充屏幕,並且每個圖像之間都有空白 這是我所看到的圖像

這是我的帶有滾動視圖代碼的表格視圖單元格

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

你可以試試這個嗎

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

問題出在您的xCoordinate計算中,contentView框架在aakeakeFromNib中將不可用。

您不應使用super.awakeFromNib來計算幀。 創建一個稱為

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

cellForRowAtIndex:調用此方法cellForRowAtIndex: UITableViewCell的委托方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM