UICollectionView

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
スクロール方向変更
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)[self.collectionView collectionViewLayout];
if(layout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
} else {
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
特定セルのみ更新(複数有)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
NSArray *indexPaths = @[indexPath];
[_collectionView reloadItemsAtIndexPaths:indexPaths];


        // 表示部分のみ更新する
        NSArray *indexPaths = collectionView.indexPathsForVisibleItems;
        [collectionView reloadItemsAtIndexPaths:indexPaths];


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
選択状態
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// 先頭を選択状態にする場合
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

これだけでは選択状態にならない場合がある

                [wself.houseListView collectionView:wself.houseListView.collectionView didSelectItemAtIndexPath:indexPath];
            });

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
スクロールのみ
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
スクロール&選択状態
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[_collectionView selectItemAtIndexPath:indexPath
                                                                       animated:YES
                                                                 scrollPosition:UICollectionViewScrollPositionBottom];                    

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
選択可否判定(デリゲート)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    return YES;
}

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
削除(アニメ有)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[_collectionItems removeObjectAtIndex:index];
[_collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
テンプレート
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


.h
-----------------------------------------------------------------------------
@property (strong, nonatomic) NSMutableArray *collectionItems;

.m viewdidload
-----------------------------------------------------------------------------
[_collectionView registerNib:[UINib nibWithNibName:@"CollectionItemCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionItemCell"];


.m delegate
-----------------------------------------------------------------------------
#pragma mark - UICollectionViewDelegate
/**
*  セクション数を返す。
*  @param collectionView 対象UICollectionView
*  @return セクション数
*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


/**
*  要素数を返す
*  @param collectionView 対象UICollectionView
*  @return 要素数
*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collectionItems count];
}


/**
*  表示対象セルを返す
*  @param collectionView 対象UICollectionView
*  @param indexPath インデックス情報
*  @return 表示対象セル
*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionItemCell"
                                                                     forIndexPath:indexPath];
    NSDictionary *data = _collectionItems[indexPath.row];
    [cell setData:data];
    
    return cell;
}


/**
*  セル選択時に呼ばれる
*  @param collectionView 対象UICollectionView
*  @param indexPath インデックス情報
*  @return 無し
*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 未使用
}
最終更新:2017年03月22日 09:33