ストーリーボード (iPad) で を含むビューを定義しました。UICollectionViewビューの下部には もありUIToolbarます。に、Core-Plot Graph (クラス) である別のビューを含む(クラスによって実装された)UICollectionViewを追加しました。UICollectionViewCelliPadCellCollectionViewCellCPTGraphHostingView
と を実装する X というクラスがUICollectionViewDelegateありUICollectionViewDataSourceます。
クラス X では、( のViewDidLoad) ビューの各セルに対して Core-Plot Graph を作成し、次のコードを使用して、グラフをUICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"ChartCollectionId";
iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
MyChart* chart = [_charts objectAtIndex:indexPath.row];
cell.hostingView.hostedGraph = chart.barChart;
return cell;
}
正常に動作しています。すべてのグラフが画面に正しく表示されています。この問題は、ユーザーがビューをスクロールすると、1 つまたは複数のセルが「空白」になるときに発生します。理由が本当にわかりません。
Interface Builder にを追加する代わりに、回避策を見つけました。CPTGraphHostingView自分UICollectionViewCellでビルドすると、前のコードは次のようになります。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"ChartCollectionId";
iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
[cell.contentView addSubview:[_hostingViews objectAtIndex:indexPath.row]];
return cell;
}
このコードでは、すべてが正常に機能しています。IBに を追加CPTGraphHostingViewしているときに機能しない理由について説明はありますか?UICollectionViewCell
メソッドが呼び出されるたびに「addSubview」を呼び出すのは問題ですか? メモリリークはありませんか?