2016-10-27 5 views
0

デバイスの向きに応じて、セクションの項目数を変更しようとしています。UICollectionViewの表示セクションのみをリロードする方法はありますか?

私はviewWillTransitionToSizeのコレクションビュー全体をリロードし、UICollectionViewのセクション数で "セクション内の最大許容アイテム"の数を変更することでそれを達成しました。

これは、私が特定のセクションをロードできることがわかるように、最良の方法ではありません。 もっと良い解決策が見つかるかどうか分かち合いましょう。

+0

表示されるアイテムのインデックスパスを取得しますctionView。それを試しましたか? – Adeel

+0

実際には目に見えるアイテムだけが再ロードされます – vadian

+0

私はこの@Adeelを試しました - (void)reloadVisibleSections {NSMutableIndexSet * visibleSections = [NSMutableIndexSet indexSet]; NSArray * arr = [[self.collectionView indexPathsForVisibleItems] valueForKey:@ "section"]; (NSNumber * arrのインデックス){[visibleSections addIndex:[index unsignedIntegerValue]]; } [self.collectionView reloadSections:visibleSections]; } デバイスを回転させたときにクラッシュするのは、風景から肖像画に移動すると表示されるセクションの数が増えるためです。 – SanjayPathak

答えて

0
NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]]; 

[self.myCollectionView reloadSections:visibleSections];あなたはこのように見える細胞を取得する必要がありますあなたがあなたの目に見えるのセクションによってはそれをしたい場合は

let table = UITableView() 
let index = IndexSet.init(integer: 0) //The section to reload 
table.reloadSections(index, with: UITableViewRowAnimation.automatic) 

+0

上記のコードはtableViewに適しています。コレクションビューの場合 - (void)reloadVisibleSections { NSMutableIndexSet * visibleSections = [NSMutableIndexSet indexSet]; NSArray * arr = [[self.collectionView indexPathsForVisibleItems] valueForKey:@ "section"]; (NSNumber * arrのインデックス)の { [visibleSections addIndex:[index unsignedIntegerValue]]; } [self.collectionView reloadSections:visibleSections]; } デバイスを回転させるとクラッシュします。 – SanjayPathak

0

あなたは以下のコードを使用して、特定のセクションをリロードすることができます table.visibleCells

これらの可視セルから再読み込みするセクションのインデックスを作成します。

let table = UITableView() 
var indexes = IndexSet() 
let visibleCells = table.visibleCells 
for cell in visibleCells { 
    indexes.insert((table.indexPath(for: cell)?.section)!) 
} 
table.reloadSections(indexes, with: UITableViewRowAnimation.automatic) 
関連する問題