UICollectiuonView中的常用方法和屬性

//通過一個(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;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市绝编,隨后出現(xiàn)的幾起案子僻澎,更是在濱河造成了極大的恐慌,老刑警劉巖十饥,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窟勃,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡逗堵,警方通過查閱死者的電腦和手機(jī)秉氧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜒秤,“玉大人汁咏,你說我怎么就攤上這事∽髅模” “怎么了攘滩?”我有些...
    開封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)纸泡。 經(jīng)常有香客問我漂问,道長(zhǎng),這世上最難降的妖魔是什么弟灼? 我笑而不...
    開封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任级解,我火速辦了婚禮,結(jié)果婚禮上田绑,老公的妹妹穿的比我還像新娘勤哗。我一直安慰自己,他們只是感情好掩驱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開白布芒划。 她就那樣靜靜地躺著冬竟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪民逼。 梳的紋絲不亂的頭發(fā)上泵殴,一...
    開封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音拼苍,去河邊找鬼笑诅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛疮鲫,可吹牛的內(nèi)容都是我干的吆你。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼俊犯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼妇多!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起燕侠,我...
    開封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤者祖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后绢彤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體七问,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年杖虾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了烂瘫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片媒熊。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奇适,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出芦鳍,到底是詐尸還是另有隱情嚷往,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布柠衅,位于F島的核電站皮仁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏菲宴。R本人自食惡果不足惜贷祈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喝峦。 院中可真熱鬧势誊,春花似錦、人聲如沸谣蠢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至挤忙,卻和暖如春霜威,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背册烈。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工戈泼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赏僧。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓矮冬,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親次哈。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胎署,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容