{"id":789,"date":"2016-10-13T11:00:52","date_gmt":"2016-10-13T03:00:52","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=789"},"modified":"2016-10-13T11:00:52","modified_gmt":"2016-10-13T03:00:52","slug":"uicollectionview%e9%bb%98%e8%ae%a4cell%e4%b8%a4%e7%ab%af%e5%af%b9%e9%bd%90%ef%bc%8c%e5%a6%82%e4%bd%95%e8%ae%a9%e5%85%b6%e5%b1%85%e5%b7%a6%e5%af%b9%e9%bd%90","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2016\/10\/13\/110052789.html","title":{"rendered":"UICollectionView\u9ed8\u8ba4Cell\u4e24\u7aef\u5bf9\u9f50\uff0c\u5982\u4f55\u8ba9\u5176\u5c45\u5de6\u5bf9\u9f50"},"content":{"rendered":"<p><strong>1\u3001\u5148\u770b\u56fe\uff1a<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/10\/Simulator-Screen-Shot-2016\u5e7410\u670813\u65e5-\u4e0a\u534810.32.42.png\" alt=\"simulator-screen-shot-2016%e5%b9%b410%e6%9c%8813%e6%97%a5-%e4%b8%8a%e5%8d%8810-32-42\" width=\"320\" height=\"568\" class=\"alignnone size-full wp-image-790\" \/><br \/>\n<strong><br \/>\n2\u3001\u57fa\u672c\u8bbe\u8ba1\u601d\u8def<\/strong><\/p>\n<p>2.1\u3001UICollectionView\u7684\u5e03\u5c40\u901a\u8fc7UICollectionViewFlowLayout\u5b9e\u73b0\uff1b<br \/>\n2.2\u3001UICollectionViewFlowLayout\u6709\u4e2a\u65b9\u6cd5<\/p>\n<pre lang=\"objc\" line=\"1\">- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect<\/pre>\n<p>2.3\u3001\u8fd9\u4e2a\u65b9\u6cd5\u662f\u8fd4\u56deUICollectionViewLayoutAttributes\u5bf9\u8c61\u6570\u7ec4\uff0c\u6bcf\u4e00\u4e2aUICollectionViewLayoutAttributes\u5bf9\u8c61\u51b3\u5b9a\u4e86\u4e00\u4e2acell\u7684\u5927\u5c0f\u4f4d\u7f6e\u53ca\u5404\u79cd\u5e03\u5c40\u5c5e\u6027\uff1b<br \/>\n2.4\u3001\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e0a\u4e0b\u624b\uff0c\u5b9e\u73b0\u5de6\u5bf9\u9f50\u5e94\u8be5\u4e0d\u662f\u4ec0\u4e48\u95ee\u9898\uff1b\u95ee\u9898\u662f\u5168\u90e8\u81ea\u5b9a\u4e49\u6bcf\u4e00\u4e2acell\u7684\u5e03\u5c40\u5417\uff1f<br \/>\n2.5\u3001\u5176\u5b9e\u6211\u4eec\u53ea\u9700\u8981\u5c06\u539f\u6709\u7684\u4e24\u7aef\u5bf9\u9f50\u6539\u6210\u5de6\u5bf9\u9f50\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u4ed6\u5e76\u4e0d\u9700\u8981\u53d8\u52a8\uff1b<br \/>\n2.6\u3001\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u53ea\u9700\u8981\u4fee\u6539frame\u7684x\u503c\u5373\u53ef\uff0c\u5176\u4ed6\u90fd\u539f\u5c01\u4e0d\u52a8\uff1b<!--more--><\/p>\n<p><strong>3\u3001\u5173\u952e\u7b97\u6cd5<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect\n{\n    NSArray *array = [super layoutAttributesForElementsInRect:rect];\n    if (array.count == 0) return array;\n    NSInteger count = array.count;\n    NSMutableArray *attributes = [NSMutableArray array];\n    [attributes addObject:[array firstObject]];\n    for (int i = 0; i < count; i++) {\n        if (i == 0) continue;\n        UICollectionViewLayoutAttributes *a0 = [array objectAtIndex:i - 1];\n        UICollectionViewLayoutAttributes *a1 = [array objectAtIndex:i];\n        CGRect frame0 = a0.frame;\n        CGRect frame1 = a1.frame;\n        \/\/ \u5f53\u524d\u8fd9\u4e2a\u4e0d\u4e3a\u884c\u5185\u9996\u4e2a\u65f6\u624d\u9700\u8981\u4fee\u6539frame\n        if (frame0.origin.y == frame1.origin.y) {\n            frame1.origin.x = CGRectGetMaxX(frame0) + 10;\n            a1.frame = frame1;\n        }\n        [attributes addObject:a1];\n    }\n    return attributes;\n}<\/pre>\n<p><strong>4\u3001Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/10\/SACollectionView.zip\">SACollectionView<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u770b\u56fe\uff1a 2\u3001\u57fa\u672c\u8bbe\u8ba1\u601d\u8def 2.1\u3001UICollectionView\u7684\u5e03\u5c40\u901a\u8fc7UICollectionViewFlowLayout\u5b9e\u73b0\uff1b 2.2\u3001UICollectionViewFlowLayout\u6709\u4e2a\u65b9\u6cd5 &#8211; (nullable NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 2.3\u3001\u8fd9\u4e2a\u65b9\u6cd5\u662f\u8fd4\u56deUICollectionViewLayoutAttributes\u5bf9\u8c61\u6570\u7ec4\uff0c\u6bcf\u4e00\u4e2aUICollectionViewLayoutAttributes\u5bf9\u8c61\u51b3\u5b9a\u4e86\u4e00\u4e2acell\u7684\u5927\u5c0f\u4f4d\u7f6e\u53ca\u5404\u79cd\u5e03\u5c40\u5c5e\u6027\uff1b 2.4\u3001\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e0a\u4e0b\u624b\uff0c\u5b9e\u73b0\u5de6\u5bf9\u9f50\u5e94\u8be5\u4e0d\u662f\u4ec0\u4e48\u95ee\u9898\uff1b\u95ee\u9898\u662f\u5168\u90e8\u81ea\u5b9a\u4e49\u6bcf\u4e00\u4e2acell\u7684\u5e03\u5c40\u5417\uff1f 2.5\u3001\u5176\u5b9e\u6211\u4eec\u53ea\u9700\u8981\u5c06\u539f\u6709\u7684\u4e24\u7aef\u5bf9\u9f50\u6539\u6210\u5de6\u5bf9\u9f50\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u4ed6\u5e76\u4e0d\u9700\u8981\u53d8\u52a8\uff1b 2.6\u3001\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u53ea\u9700\u8981\u4fee\u6539frame\u7684x\u503c\u5373\u53ef\uff0c\u5176\u4ed6\u90fd\u539f\u5c01\u4e0d\u52a8\uff1b<\/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":[15,17],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-project","tag-module","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/789","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=789"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}