{"id":437,"date":"2016-10-31T07:45:30","date_gmt":"2016-10-30T22:45:30","guid":{"rendered":"http:\/\/blue-bear.jp\/kb\/?p=437"},"modified":"2016-10-31T07:46:06","modified_gmt":"2016-10-30T22:46:06","slug":"swift-collectionview%e3%81%ae%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/swift-collectionview%e3%81%ae%e5%ae%9f%e8%a3%85\/","title":{"rendered":"[swift] CollectionView\u306e\u5b9f\u88c5"},"content":{"rendered":"<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/p>\n<pre>\r\nclass collectionViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{\r\n\r\nlet array = [\"1\",\"2\",\"3\",\"4\"]\r\n\r\noverride func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \r\n        collectionView.delegate = self\r\n        collectionView.dataSource = self\r\n}\r\n\r\n    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{\r\n\r\n        \/\/Cell\u3068\u3044\u3046identifer\u306b\u4e88\u3081StoryBoard\u3067\u8a2d\u5b9a\u3057\u3066\u304a\u304f\r\n        let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(\"Cell\", forIndexPath: indexPath)\r\n        let row = indexPath.row\r\n        \r\n        \/\/\u30bf\u30b0\uff11\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u53d6\u5f97\r\n        let contentView = cell.contentView.viewWithTag(1) as! UIImageView\r\n        contentView.image = UIImage(named:\"image.png\")\r\n\r\n        \/\/\u30bf\u30b0\uff12\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u53d6\u5f97\r\n        let label = cell.contentView.viewWithTag(2) as! UILabel\r\n        label.text = String(array[row])\r\n        \r\n        return cell\r\n    }\r\n    \r\n    \/\/\u30bb\u30eb\u306e\u30b5\u30a4\u30ba \u30b3\u30fc\u30c9\u3067\u306f\u9ad8\u3055\uff17\uff15\u3001\u6a2a\u5e45\u306f\u753b\u9762\u534a\u5206\u306e-12\u3067\u8abf\u6574\u3057\u3066\u3044\u308b\r\n    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {\r\n        let cellHeight:CGFloat = 75.00\r\n        let cellWidth:CGFloat = self.view.frame.size.width\/2-12\r\n        return CGSizeMake(cellWidth, cellHeight)\r\n    }\r\n \r\n    \/\/\u30bb\u30eb\u306e\u9593\u9694\u6307\u5b9a  \r\n    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {\r\n        return UIEdgeInsetsMake(5, 7, 5, 7)\r\n    }\r\n    \r\n    \/\/\u30bb\u30af\u30b7\u30e7\u30f3\u6570\r\n    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {\r\n        return 1\r\n    }\r\n\r\n    \/\/ \u8981\u7d20\u6570\u3092\u5165\u308c\u308b\u3001\u8981\u7d20\u4ee5\u4e0a\u306e\u6570\u5b57\u3092\u5165\u308c\u308b\u3068\u8868\u793a\u3067\u30a8\u30e9\u30fc\u3068\u306a\u308b\r\n    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\r\n        return array.count;\r\n    }\r\n    \r\n    \/\/\u30bf\u30c3\u30d7\u3055\u308c\u305f\u6642\u306e\u6319\u52d5\r\n    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {\r\n        let row = indexPath.row\r\n        \/\/\u305d\u306e\u4ed6\u30e1\u30bd\u30c3\u30c9\r\n        \r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9 class collec<\/p>\n","protected":false},"author":1,"featured_media":88,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/437"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":2,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":439,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/437\/revisions\/439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media\/88"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}