質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1750閲覧

registerしたはずのcellが反映されない

amazon_106

総合スコア50

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/04/06 16:02

編集2019/04/07 01:06

完成イメージ

現在
イメージ説明

やりたいこと
下部のグレーのcellをsimulatorに反映させたい
現在はSelectPhotoCellのプログラムが反映されていない状態です。

SelectImage.VC

import UIKit import Photos private let reuseIdentifier = "SelectPhotoCell" private let headerIdentifier = "SelectPhotoHeader" class SelectImageVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { // MARK: - Properties var images = [UIImage]() var assets = [PHAsset]() var selectedImage: UIImage? var header: SelectPhotoHeader? override func viewDidLoad() { super.viewDidLoad() // register cell classes // 下部の選択画面の登録 collectionView?.register(SelectPhotoCell.self, forCellWithReuseIdentifier: reuseIdentifier) // 上部の大画面の登録 collectionView?.register(SelectPhotoHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier) collectionView.backgroundColor = .white configureNavigationButtons() fetchPhotos() } // cellのサイズ // MARK: - UICollectionViewFlowLayout UICollectionViewDelegateFlowLayoutを適用させた上で サイズ指定できる // referenceSizeForHeaderInSection header(上部の大きい)のcellについてのサイズ指定 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { // 幅 let width = view.frame.width // 正方形 return CGSize(width: width, height: width) } // 複数cellのサイズ指定 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // 3という余白(区切り線)を作り,一行を4つのcellで割りたい(作りたい) let width = (view.frame.width - 3) / 4 // 正方形 return CGSize(width: width, height: width) } //セルの水平方向のマージンを設定 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 1 } //セルの垂直方向のマージンを設定 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 1 } // MARK: - UICollectionViewDataSource // セクションの数 override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } // セルの個数 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return images.count } // header(選択し表示される大きい画面)の内容を決める override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { // 登録したヘッダーを取得 let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! SelectPhotoHeader self.header = header if let selectedImage = self.selectedImage { // index selected image 画質を良くする if let index = self.images.index(of: selectedImage) { // asset associated with selected image let selectedAsset = self.assets[index] let imageManager = PHImageManager.default() let targetSize = CGSize(width: 600, height: 600) // request image imageManager.requestImage(for: selectedAsset, targetSize: targetSize, contentMode: .default, options: nil) { (image, info) in header.photoImageView.image = image } } } return header } // cellの内容を決める override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // as! cellをSelectPhotoCellの型に変換(ダウンキャスト)する ????? // privateで指定した reuseIdentifier = "SelectPhotoCell"を呼んでいる // 登録した下部のcellを取得する let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SelectPhotoCell cell.photoImageView.image = images[indexPath.row] return cell } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { self.selectedImage = images[indexPath.row] self.collectionView?.reloadData() let indexPath = IndexPath(item: 0, section: 0) collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true) } // MARK: - Handlers 上部のnavbar @objc func handleCancel() { // モーダルウィンドウを閉じる self.dismiss(animated: true, completion: nil) } @objc func handleNext() { let uploadPostVC = UploadPostVC() uploadPostVC.selectedImage = header?.photoImageView.image navigationController?.pushViewController(uploadPostVC, animated: true) } func configureNavigationButtons() { navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel)) navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(handleNext)) } func getAssetFetchOptios() -> PHFetchOptions { let options = PHFetchOptions() // fetch limit 30枚のみ取得する options.fetchLimit = 30 // sort photos by date 一番上から最新順に並べる let sortDescritor = NSSortDescriptor(key: "creationDate", ascending: false) // set sort descriptor for options options.sortDescriptors = [sortDescritor] // return options return options } func fetchPhotos() { let allPhotos = PHAsset.fetchAssets(with: .image, options: getAssetFetchOptios()) print("機能が機能している") // fetch images on background thread DispatchQueue.global(qos: .background).async { // enumerate objects allPhotos.enumerateObjects({ (asset, count, stop) in print("Count is (count)") let imageManager = PHImageManager.default() let targetSize = CGSize(width: 200, height: 200) let options = PHImageRequestOptions() options.isSynchronous = true // request image reprentation for specified asset imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in if let image = image{ // append image to data source self.images.append(image) // append assets to data source self.assets.append(asset) // set selected image with first image if self.selectedImage == nil { self.selectedImage = image } // reload collection view with images once count has completed if count == allPhotos.count - 1 { // reload collection view on main thread DispatchQueue.main.async { self.collectionView.reloadData() } } } }) }) } } }

SelectPhotoCell

import UIKit // 画像選択、下部の複数の画像 class SelectPhotoCell: UICollectionViewCell { let photoImageView: UIImageView = { let iv = UIImageView() iv.contentMode = .scaleAspectFill iv.clipsToBounds = true iv.backgroundColor = .lightGray return iv }() override init(frame: CGRect) { super.init(frame: frame) addSubview(photoImageView) photoImageView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

全くわからない状態です。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/04/06 23:09

しつもんはなんでしょうか
guest

回答1

0

自己解決

他のSimulatorでビドしたら、無事表示されました。
「写真を許可しますか?」というポップアップが出ました。それを許可したら写真が表示されました。

投稿2019/04/07 01:38

amazon_106

総合スコア50

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問