{"id":570,"date":"2014-05-30T16:09:21","date_gmt":"2014-05-30T08:09:21","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=570"},"modified":"2016-07-15T17:28:40","modified_gmt":"2016-07-15T09:28:40","slug":"uicollectionview%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%88%e4%b8%8euitableview%e7%9a%84%e6%af%94%e8%be%83%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/05\/30\/160921570.html","title":{"rendered":"UICollectionView\u7684\u4f7f\u7528\uff08\u4e0eUITableView\u7684\u6bd4\u8f83\uff09"},"content":{"rendered":"<p><strong>1\u3001\u5148\u770b\u4e24\u8005\u6bd4\u8f83\u7684\u6548\u679c\u56fe<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-571\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/160302_4zyd_865233.png\" alt=\"160302_4zyd_865233\" width=\"280\" height=\"420\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-572\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-\u6a21\u62df\u5668\u5c4f\u5e55\u5feb\u7167\u201c2014\u5e745\u670830\u65e5-\u4e0b\u53483.20.46\u201d.png\" alt=\"iOS \u6a21\u62df\u5668\u5c4f\u5e55\u5feb\u7167\u201c2014\u5e745\u670830\u65e5 \u4e0b\u53483.20.46\u201d\" width=\"280\" height=\"420\" \/><\/p>\n<p><strong>2\u3001\u7279\u70b9\uff1a<\/strong><\/p>\n<p>2.1\u3001\u90fd\u7ee7\u627f\u81eaUIScrollView\uff0c\u56e0\u6b64\u8fd9\u4e24\u8005\u53ef\u4ee5\u8bf4\u662f\u5144\u5f1f\u5173\u7cfb\uff0c\u90fd\u662f\u7528\u4e8e\u6570\u636e\u5c55\u793a\u65b9\u9762\u90fd\u662f\u975e\u51e1\u7684\u8868\u73b0\uff0c\u5e76\u4e14UICollectionView\u81eaiOS6\u4ee5\u6765\u624d\u652f\u6301\uff1b<\/p>\n<p>2.2\u3001Collection View\u7684\u6574\u4f53\u6784\u6210\u5143\u7d20\uff0c\u5171\u6709\u4e09\u4e2a\u8981\u7d20\uff0c\u5206\u522b\u5982\u4e0b<\/p>\n<ul>\n<li>Cells(\u5355\u5143\u683c)<\/li>\n<li>Supplementary Views\uff08\u8865\u5145\u7684view\uff0c\u76f8\u5f53\u4e8eTableView\u7684\u9875\u7709\u548c\u9875\u811a\uff09<\/li>\n<li>Decoration Views\uff08\u88c5\u9970View\uff0c\u7528\u4e8e\u88c5\u9970\u6574\u4e2aCollectionView\u7684<\/li>\n<\/ul>\n<p>2.3\u3001\u6570\u636e\u6a21\u578b(\u6570\u636e\u63d0\u4f9b\u8005UICollectionViewDataSource)\uff0c\u4e3b\u8981\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>Section\u6570\u76ee<\/li>\n<li>Section\u91cc\u9762\u6709\u591a\u5c11item<\/li>\n<li>\u63d0\u4f9bCell\u548csupplementary view\u8bbe\u7f6e<\/li>\n<\/ul>\n<p>\u5206\u522b\u4e3a\uff1a<!--more--><\/p>\n<pre lang=\"objc\" line=\"1\">numberOfSectionsInCollectionView:\n\ncollectionView:numberOfItemsInSection:\n\ncollectionView:cellForItemAtIndexPath:<\/pre>\n<p>2.4\u3001Cell\u548cView\u7684\u91cd\u7528<br \/>\n\u5728iOS6\u4e2d\uff0cCell\u91cd\u7528\u6539\u5584\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u7684\u4f7f\u7528Cell\uff0c\u7cfb\u7edf\u603b\u662f\u4e3a\u6211\u4eec\u521d\u59cb\u5316Cell\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u53ea\u9700\u8981\u7b80\u5355\u7684\u6309\u7167\u4e24\u6b65\u8d70\u5373\u53ef\uff1a<\/p>\n<p>1\uff09 \u5fc5\u987b\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u8fdb\u884cCell\u7c7b\u7684\u6ce8\u518c\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">- (void)registerClass:forCellWithReuseIdentifier:\n\n- (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:\n\n- (void)registerNib:forCellWithReuseIdentifier:\n\n- (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:<\/pre>\n<p>2\uff09 \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2aCell,\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">-(id)dequeueReusableCellWithReuseIdentifier:forIndexPath:\n\n-(id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:<\/pre>\n<p>2.5\u3001\u754c\u9762\u5e03\u5c40<br \/>\ncollectionView\u4e0etableView\u6700\u5927\u7684\u4e0d\u540c\u70b9\u5c31\u5728\u4e8e\u6b64\uff0ccollectionView\u5fc5\u987b\u8981\u4f7f\u7528\u81ea\u5df1\u7684layout\uff08UICollectionViewLayout\uff09<br \/>\n\u5982\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];\nflowLayout.itemSize = CGSizeMake(52, 52);               \/\/ cell\u5927\u5c0f\nflowLayout.minimumInteritemSpacing = 1;                 \/\/ cell\u95f4\u8ddd\nflowLayout.minimumLineSpacing = 1;                      \/\/ cell\u884c\u8ddd\nflowLayout.sectionInset = (UIEdgeInsets){81,1,1,1};     \/\/ cell\u8fb9\u8ddd<\/pre>\n<p>\u521b\u5efacollectionView\u9700\u8981\u5e26Layout\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;<\/pre>\n<p><strong>3\u3001\u793a\u4f8b<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  CarLicenseView.m\n\/\/  RouteMapInformation\n\/\/\n\/\/  Created by yusian on 14-5-29.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import \"CarLicenseView.h\"\n\n\/****************************Cell\u5b9a\u4e49*********************************\/\n\n@interface SALicenseCell : UICollectionViewCell\n\n@property (nonatomic, strong) UILabel *label;\n\n@end\n\n@implementation SALicenseCell\n\n- (id)initWithFrame:(CGRect)frame\n{\n    self = [super initWithFrame:frame];\n    if (self) {\n        \n        \/\/ \u952e\u76d8\u6587\u5b57\u8bbe\u7f6e\n        self.label = [[UILabel alloc] init];\n        self.label.backgroundColor = [UIColor clearColor];  \/\/ \u900f\u660e\u989c\u8272\n        self.label.textAlignment = NSTextAlignmentCenter;   \/\/ \u6587\u5b57\u5c45\u4e2d\n        self.label.frame = CGRectMake(0, 0, 52, 52);        \/\/ \u4f4d\u7f6e\u5927\u5c0f\n        self.label.font = [UIFont systemFontOfSize:20];     \/\/ \u6587\u5b57\u5927\u5c0f\n        self.backgroundColor = [UIColor whiteColor];        \/\/ \u80cc\u666f\u989c\u8272\n        \n        \n        [self.contentView addSubview:self.label];\n    }\n    return self;\n}\n\n+ (NSString *)ID\n{\n    return @\"licenseCellID\";\n}\n\n@end\n\n\/****************************Cell\u5b9a\u4e49*********************************\/\n\n@interface CarLicenseView () &lt;UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate&gt;\n\n@end\n\n@implementation CarLicenseView\n\n#pragma mark \u754c\u9762\u521d\u59cb\u5316\n- (id)initWithFrame:(CGRect)frame\n{\n    \/\/ 1\u3001\u952e\u76d8\u6309\u94ae\u5e03\u5c40\n    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];\n    flowLayout.itemSize = CGSizeMake(52, 52);               \/\/ \u6309\u952e\u5927\u5c0f\n    flowLayout.minimumInteritemSpacing = 1;                 \/\/ \u6309\u952e\u95f4\u8ddd\n    flowLayout.minimumLineSpacing = 1;                      \/\/ \u6309\u952e\u884c\u8ddd\n    flowLayout.sectionInset = (UIEdgeInsets){81,1,1,1};     \/\/ \u6309\u952e\u8fb9\u8ddd\n    \n    \/\/ 2\u3001\u952e\u76d8\u9644\u52a0\u89c6\u56fe\n    self = [super initWithFrame:frame collectionViewLayout:flowLayout];\n    if (self) {\n        \/\/ \u952e\u76d8\u80cc\u666f\u8272\n        self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];\n        \n    }\n    \n    \/\/ 3\u3001\u6ce8\u518c\u6309\u94ae\u7c7b\uff08\u5185\u90e8\u5904\u7406\u673a\u5236\uff09\n    [self registerClass:[SALicenseCell class] forCellWithReuseIdentifier:[SALicenseCell ID]];\n    self.delegate = self;\n    self.dataSource = self;\n    \n    return self;\n}\n\n#pragma mark \u754c\u9762\u7ed8\u5236\u65b9\u6cd5\n#pragma mark \u952e\u76d8\u6309\u952e\u6570\u76ee\n- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section\n{\n    return 30;\n}\n\n#pragma mark \u952e\u76d8\u6309\u952e\u6587\u5b57\n- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath\n{\n    SALicenseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SALicenseCell ID] forIndexPath:indexPath];\n    cell.backgroundColor = [UIColor blueColor];\n    return cell;\n}\n@end<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u770b\u4e24\u8005\u6bd4\u8f83\u7684\u6548\u679c\u56fe 2\u3001\u7279\u70b9\uff1a 2.1\u3001\u90fd\u7ee7\u627f\u81eaUIScrollView\uff0c\u56e0\u6b64\u8fd9\u4e24\u8005\u53ef\u4ee5\u8bf4\u662f\u5144\u5f1f\u5173\u7cfb\uff0c\u90fd\u662f\u7528\u4e8e\u6570\u636e\u5c55\u793a\u65b9\u9762\u90fd\u662f\u975e\u51e1\u7684\u8868\u73b0\uff0c\u5e76\u4e14UICollectionView\u81eaiOS6\u4ee5\u6765\u624d\u652f\u6301\uff1b 2.2\u3001Collection View\u7684\u6574\u4f53\u6784\u6210\u5143\u7d20\uff0c\u5171\u6709\u4e09\u4e2a\u8981\u7d20\uff0c\u5206\u522b\u5982\u4e0b Cells(\u5355\u5143\u683c) Supplementary Views\uff08\u8865\u5145\u7684view\uff0c\u76f8\u5f53\u4e8eTableView\u7684\u9875\u7709\u548c\u9875\u811a\uff09 Decoration Views\uff08\u88c5\u9970View\uff0c\u7528\u4e8e\u88c5\u9970\u6574\u4e2aCollectionView\u7684 2.3\u3001\u6570\u636e\u6a21\u578b(\u6570\u636e\u63d0\u4f9b\u8005UICollectionViewDataSource)\uff0c\u4e3b\u8981\u529f\u80fd\uff1a Section\u6570\u76ee Section\u91cc\u9762\u6709\u591a\u5c11item \u63d0\u4f9bCell\u548csupplementary view\u8bbe\u7f6e \u5206\u522b\u4e3a\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}