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

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

新規登録して質問してみよう
ただいま回答率
85.50%
iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

0回答

2320閲覧

全てソースコードで実装するCollectionViewについて

pinokio

総合スコア32

iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

1クリップ

投稿2015/10/18 07:24

CollectionViewのセルにPhotosで取得したカメラロールの写真を表示しようとしています。
その際に以下のようなコードを書いているのですが、CollectionViewに写真が表示されません。

import UIKit import Photos class UserPhotosCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { var photoAssets = [PHAsset]() override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { super.init(frame: frame, collectionViewLayout: layout) //デリゲート self.delegate = self self.dataSource = self //セルを登録 self.registerNib(UINib(nibName: "UserPhotosCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "UserPhotosCollectionViewCell") self.backgroundColor = UIColor.whiteColor() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func numberOfSections() -> Int { return 1 } //呼ばれてない func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = dequeueReusableCellWithReuseIdentifier("UserPhotosCollectionViewCell", forIndexPath: indexPath) as! UserPhotosCollectionViewCell let asset = photoAssets[indexPath.row] let manager = PHImageManager() manager.requestImageForAsset(asset, targetSize: CGSize(width: 70, height: 70), contentMode: .AspectFill, options: nil) { (image, info) -> Void in cell.photoImage.image = image } return cell } //呼ばれている func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return photoAssets.count }
import UIKit import Photos class ChoosePhotoViewController: UIViewController { var photoAssets = [PHAsset]() override func viewDidLoad() { super.viewDidLoad() self.checkAuthorizationStatus() self.view.backgroundColor = UIColor.lightGrayColor() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //フォトライブラリへのアクセス許可を求める private func checkAuthorizationStatus() { let status = PHPhotoLibrary.authorizationStatus() switch status { case .Authorized: self.getAllPhotosInfo() default: PHPhotoLibrary.requestAuthorization({ (status) -> Void in if status == .Authorized { self.getAllPhotosInfo() } }) } } //フォトライブラリから全ての写真のPHAssetオブジェクトを取得する private func getAllPhotosInfo() { photoAssets = [] //ソート条件を指定 let options = PHFetchOptions() options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] let assets: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options) assets.enumerateObjectsUsingBlock { (asset, index, stop) -> Void in self.photoAssets.append(asset as! PHAsset) let frame = CGRect(x: 0, y: self.view.frame.height - self.view.frame.height / 2, width: self.view.frame.width, height: self.view.frame.height / 2) let layout = UICollectionViewLayout() let userPhotosCollectionView = UserPhotosCollectionView(frame: frame, collectionViewLayout: layout) print(userPhotosCollectionView) userPhotosCollectionView.photoAssets = self.photoAssets self.view.addSubview(userPhotosCollectionView) } } }

デバッグエリアには以下のように表示されています。

<InstagramApp.UserPhotosCollectionView: 0x7f9e6408be00; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e6269ce80>; layer = <CALayer: 0x7f9e6267add0>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626777a0> <InstagramApp.UserPhotosCollectionView: 0x7f9e6405a600; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b2b40>; layer = <CALayer: 0x7f9e626b2670>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b1eb0> <InstagramApp.UserPhotosCollectionView: 0x7f9e640a8000; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b4b40>; layer = <CALayer: 0x7f9e626b4290>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b4540> <InstagramApp.UserPhotosCollectionView: 0x7f9e640a8800; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e626b6840>; layer = <CALayer: 0x7f9e626b6090>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e626b6340> <InstagramApp.UserPhotosCollectionView: 0x7f9e62884c00; baseClass = UICollectionView; frame = (0 333.5; 375 333.5); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9e624d4f70>; layer = <CALayer: 0x7f9e624d52f0>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <UICollectionViewLayout: 0x7f9e62487ec0>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問