//通過一個(gè)布局策略初識(shí)化CollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
//獲取和設(shè)置collection的layout
@property (nonatomic, strong) UICollectionViewLayout *collectionViewLayout;
//數(shù)據(jù)源和代理
@property (nonatomic, weak, nullable) id <UICollectionViewDelegate> delegate;
@property (nonatomic, weak, nullable) id <UICollectionViewDataSource> dataSource;
//從一個(gè)class或者xib文件進(jìn)行cell(item)的注冊(cè)
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
//下面兩個(gè)方法與上面相似拉馋,這里注冊(cè)的是頭視圖或者尾視圖的類
//其中第二個(gè)參數(shù)是設(shè)置 頭視圖或者尾視圖 系統(tǒng)為我們定義好了這兩個(gè)字符串
//UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader NS_AVAILABLE_IOS(6_0);
//UIKIT_EXTERN NSString *const UICollectionElementKindSectionFooter NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
//這兩個(gè)方法是從復(fù)用池中取出cell或者頭尾視圖
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
//設(shè)置是否允許選中 默認(rèn)YES
@property (nonatomic) BOOL allowsSelection;
//設(shè)置是否允許多選 默認(rèn)NO
@property (nonatomic) BOOL allowsMultipleSelection;
//獲取所有選中的item的位置信息
- (nullable NSArray<NSIndexPath *> *)indexPathsForSelectedItems;
//設(shè)置選中某一item台谍,并使視圖滑動(dòng)到相應(yīng)位置,scrollPosition是滑動(dòng)位置的相關(guān)參數(shù),如下:
/*
typedef NS_OPTIONS(NSUInteger, UICollectionViewScrollPosition) {
//無
UICollectionViewScrollPositionNone = 0,
//垂直布局時(shí)使用的 對(duì)應(yīng)上中下
UICollectionViewScrollPositionTop = 1 << 0,
UICollectionViewScrollPositionCenteredVertically = 1 << 1,
UICollectionViewScrollPositionBottom = 1 << 2,
//水平布局時(shí)使用的 對(duì)應(yīng)左中右
UICollectionViewScrollPositionLeft = 1 << 3,
UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
UICollectionViewScrollPositionRight = 1 << 5
};
*/
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
//將某一item取消選中
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
//重新加載數(shù)據(jù)
- (void)reloadData;
//下面這兩個(gè)方法散罕,可以重新設(shè)置collection的布局,后面的方法多了一個(gè)布局完成后的回調(diào)维哈,iOS7后可以用
//使用這兩個(gè)方法可以產(chǎn)生非常炫酷的動(dòng)畫效果
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated;
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
//下面這些方法更加強(qiáng)大雄驹,我們可以對(duì)布局更改后的動(dòng)畫進(jìn)行設(shè)置
//這個(gè)方法傳入一個(gè)布局策略layout,系統(tǒng)會(huì)開始進(jìn)行布局渲染,返回一個(gè)UICollectionViewTransitionLayout對(duì)象
//這個(gè)UICollectionViewTransitionLayout對(duì)象管理動(dòng)畫的相關(guān)屬性床未,我們可以進(jìn)行設(shè)置
- (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout completion:(nullable UICollectionViewLayoutInteractiveTransitionCompletion)completion NS_AVAILABLE_IOS(7_0);
//準(zhǔn)備好動(dòng)畫設(shè)置后褂乍,我們需要調(diào)用下面的方法進(jìn)行布局動(dòng)畫的展示,之后會(huì)調(diào)用上面方法的block回調(diào)
- (void)finishInteractiveTransition NS_AVAILABLE_IOS(7_0);
//調(diào)用這個(gè)方法取消上面的布局動(dòng)畫設(shè)置即硼,之后也會(huì)進(jìn)行上面方法的block回調(diào)
- (void)cancelInteractiveTransition NS_AVAILABLE_IOS(7_0);
//獲取分區(qū)數(shù)
- (NSInteger)numberOfSections;
//獲取某一分區(qū)的item數(shù)
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
//下面兩個(gè)方法獲取item或者頭尾視圖的layout屬性逃片,這個(gè)UICollectionViewLayoutAttributes對(duì)象
//存放著布局的相關(guān)數(shù)據(jù),可以用來做完全自定義布局只酥,后面博客會(huì)介紹
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
//獲取某一點(diǎn)所在的indexpath位置
- (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;
//獲取某個(gè)cell所在的indexPath
- (nullable NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell;
//根據(jù)indexPath獲取cell
- (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//獲取所有可見cell的數(shù)組
- (NSArray<__kindof UICollectionViewCell *> *)visibleCells;
//獲取所有可見cell的位置數(shù)組
- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems;
//下面三個(gè)方法是iOS9中新添加的方法褥实,用于獲取頭尾視圖
- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
- (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);
- (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);
//使視圖滑動(dòng)到某一位置,可以帶動(dòng)畫效果
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
//下面這些方法用于動(dòng)態(tài)添加裂允,刪除损离,移動(dòng)某些分區(qū)獲取items
- (void)insertSections:(NSIndexSet *)sections;
- (void)deleteSections:(NSIndexSet *)sections;
- (void)reloadSections:(NSIndexSet *)sections;
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
UICollectiuonView中的常用方法和屬性
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜒秤,“玉大人汁咏,你說我怎么就攤上這事∽髅模” “怎么了攘滩?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)纸泡。 經(jīng)常有香客問我漂问,道長(zhǎng),這世上最難降的妖魔是什么弟灼? 我笑而不...
- 正文 為了忘掉前任级解,我火速辦了婚禮,結(jié)果婚禮上田绑,老公的妹妹穿的比我還像新娘勤哗。我一直安慰自己,他們只是感情好掩驱,可當(dāng)我...
- 文/花漫 我一把揭開白布芒划。 她就那樣靜靜地躺著冬竟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪民逼。 梳的紋絲不亂的頭發(fā)上泵殴,一...
- 文/蒼蘭香墨 我猛地睜開眼俊犯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼妇多!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起燕侠,我...
- 序言:老撾萬榮一對(duì)情侶失蹤者祖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后绢彤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體七问,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年杖虾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了烂瘫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片媒熊。...
- 正文 年R本政府宣布柠衅,位于F島的核電站皮仁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏菲宴。R本人自食惡果不足惜贷祈,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喝峦。 院中可真熱鬧势誊,春花似錦、人聲如沸谣蠢。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至挤忙,卻和暖如春霜威,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背册烈。 一陣腳步聲響...
- 正文 我出身青樓矮冬,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親次哈。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胎署,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 【蝴蝶效應(yīng)】 蝴蝶效應(yīng):上個(gè)世紀(jì)70年代哀卫,美國(guó)一個(gè)名叫洛倫茲的氣象學(xué)家在解釋空氣系統(tǒng)理論時(shí)說巨坊,亞馬遜雨林一只蝴蝶...
- 原文地址:http://www.cnblogs.com/txw1958/p/weixin-access-token...
- 最近加入了濟(jì)南行動(dòng)派社群趾撵,群里的小伙伴們個(gè)個(gè)都是身懷絕技的武林高手,或攝影能手共啃,或手賬達(dá)人占调,或讀書分享者,或原創(chuàng)作...
- “分手再和好的幾率有多大?” 文:福爾摩九 1. 刷朋友圈看到張哥宣布分手了纵苛,想起前不久還和我說和女朋友又復(fù)合了剿涮。...