前提・実現したいこと
UICollectionViewで1行目は横幅一杯に1枚、2行目以降は1行に2枚画像を表示する画面を開発しています。
発生している問題・エラーメッセージ
初回表示はされるですが、画面を下にスクロールし、再度一番上に戻ると1行目の横幅が半分になり、途中の行の片方の画像の横幅が広がってしまいます。スクロールしても最初に表示された状態を保つためにはどのようにすべきでしょうか。
###該当のソースコード
PagingMenuControllerを使用しており、メインのビューコントロラーからimageUrlList
をセットしてMyViewController
を呼び出しています
swift
1class MyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 2 3 var imageUrlList: Array<String>! 4 5 override func loadView() { 6 super.loadView() 7 // Do any additional setup after loading the view, typically from a nib. 8 9 let myCollectionView = UICollectionView(frame: CGRect(origin: CGPoint.zero, size: self.view.frame.size), collectionViewLayout: UICollectionViewFlowLayout()) 10 myCollectionView.backgroundColor = UIColor.white 11 myCollectionView.register(MyCollection.self, forCellWithReuseIdentifier: "MyCell") 12 13 myCollectionView.delegate = self 14 myCollectionView.dataSource = self 15 16 self.view.addSubview(myCollectionView) 17 } 18 19 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 20 if indexPath.row == 0 { 21 return CGSize(width: collectionView.frame.width, height: collectionView.frame.height * 0.3) 22 } 23 24 return CGSize(width: collectionView.frame.width * 0.5, height: collectionView.frame.height * 0.3) 25 } 26 27 28 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 29 30 let collection = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCollection 31 let url = imageUrlList[indexPath.row] 32 33 let thumbnail = collection.contentView.viewWithTag(MyCollection.TAG_THUMBNAIL) as! ThumbnailImageView 34 thumbnail.loadImage(urlString: url) 35 36 return collection 37 } 38} 39 40class MyCollection: UICollectionViewCell { 41 42 static let TAG_THUMBNAIL: Int = 0 43 44 required init(coder aDecoder: NSCoder) { 45 super.init(coder: aDecoder)! 46 } 47 48 override init(frame: CGRect) { 49 super.init(frame: frame) 50 51 initView(width: frame.size.width, height: frame.size.height) 52 } 53 54 func initView(width: CGFloat, height: CGFloat) { 55 56 let thumbnail: ThumbnailImageView = ThumbnailImageView(frame: CGRect(x: 0, y: 0, width: width, height: height * 0.3)) 57 thumbnail.tag = MyCollection.TAG_THUMBNAIL 58 self.contentView.addSubview(thumbnail) 59 } 60} 61 62class ThumbnailImageView: UIImageView { 63 64 let CACHE_SEC: TimeInterval = 60 * 60 * 24 // 1 day 65 66 var baseWidth: CGFloat = 0 67 var baseHeight: CGFloat = 0 68 69 required init(coder aDecoder: NSCoder) { 70 super.init(coder: aDecoder)! 71 } 72 73 override init (frame: CGRect) { 74 super.init(frame: frame) 75 } 76 77 func loadImage(urlString: String) { 78 self.alpha = 0 79 let req = URLRequest(url: NSURL(string: urlString)! as URL, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: CACHE_SEC); 80 let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main); 81 82 session.dataTask(with: req, completionHandler: { (data, resp, err) in 83 if ((err) == nil) { 84 85 // Success 86 self.image = UIImage(data: data!); 87 88 UIView.animate(withDuration: 1.0) { () -> Void in 89 self.alpha = 1 90 } 91 92 } else { 93 // TODO: show default if error 94 print("AsyncImageView:Error \(err?.localizedDescription)"); 95 } 96 }).resume(); 97 } 98}
###試したこと
セルのサイズを変更しているcollectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
の引数のindexPathが描画したセルの順番と合っていないのかと疑いブレークポイントを貼ってデバッグしましたが、スクロール時にブレークポイントに引っかかりませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
- Swift 3
- xcode 8.2.1
回答2件
あなたの回答
tips
プレビュー