2014年7月21日月曜日

【開発】UITableView,UICollectionViewのCellからIndexPathを知る方法!

開発していて現在表示されているCellだけ更新した場合がある。
その場合、下記で取得できる。


[self.collectionView visibleCells]

だが、さらにIndexPathがわからないと更新できない。

CellからIndexPathを知りたい場合は下記で

[self.collectionView indexPathForCell:cell]

そして更新する場合は下記です。

[self.collectionView reloadItemsAtIndexPaths:indexPaths]


これらを組み合わせて下記のようなメソッドを書いてviewDidAppearとかで呼んでます。

-(void)allCellReload
{
    NSMutableArray * indexPaths = [NSMutableArray new];
    for (UICollectionViewCell * cell in [self.collectionView visibleCells]) {
        [indexPaths addObject:[self.collectionView indexPathForCell:cell]];
    }
    [self.collectionView reloadItemsAtIndexPaths:indexPaths];
}

以上!!

0 件のコメント:

コメントを投稿