私はcollectionViewに約14個のアイテムがあり、スクロールは全くスムーズではありません。私は自動レイアウトを使用しています(私の制約はエラーを記録しません)、私はcornerRadiusプロパティを使用しています(メッセージングアプリケーションです)。UICollectionView Scrollパフォーマンス恐ろしい
私は時間プロファイラ機器を使用して、それを分析してみた、これは私がスクロールしたときに見たものだった。
は残念ながらグレー表示されている「Xcodeで明らかに」、私はできませんどのラインが永遠に採取されているか正確に見てください。私は各項目のサイズを計算しています。私は巨大な9377ミリ秒の遅延が何であるか疑問に思っていますか?私が正しくリコールするならば、私は順序付きセットを使用していません。
ここに私のcellForItemAtIndexpath
方法です:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//Define Message
Message *message = (Message *)[self.messages objectAtIndex:indexPath.item];
//Check Message Type
switch (message.type) {
case MessageTypeText: {
//Initialize Cell
TextMessageItem *cell = (TextMessageItem *)[collectionView dequeueReusableCellWithReuseIdentifier:kTextMessageItemIdentifier forIndexPath:indexPath];
//Return Cell
return cell;
break;
}
}
return nil;
}
は、ここで私は高さを計算しています方法は次のとおりです。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//Retrieve the right message
Message *message = (Message *)[self.messages objectAtIndex:indexPath.row];
//Check Message Type
switch (message.type) {
case MessageTypeText: {
//Create a temporary cell to get the correct size
TextMessageItem *tempCell = [[TextMessageItem alloc] init];
//Configure the cell
[self collectionView:collectionView configureTextCell:tempCell atIndexPath:indexPath];
CGSize s = [tempCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow];
//Return Content Size
return CGSizeMake(self.collectionView.bounds.size.width, s.height);
break;
}
}
//Return
return CGSizeMake(0, 0);
}
マイWillDisplayCell
方法:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
//Retrieve message from our array of messages
Message *message = (Message *)[self.messages objectAtIndex:indexPath.row];
//Check Message Type
switch (message.type) {
case MessageTypeText: {
//Cast Cell
TextMessageItem *textItem = (TextMessageItem *)cell;
[self collectionView:collectionView configureTextCell:textItem atIndexPath:indexPath];
break;
}
}
}
そして最後に、これは私の習慣ですが、 configureTextCell
方法:
- (void)collectionView:(UICollectionView *)collectionView configureTextCell:(TextMessageItem *)item atIndexPath:(NSIndexPath *)indexPath {
//Retrieve Message
Message *message = (Message *)[self.messages objectAtIndex:indexPath.item];
//Display Timestamp if Needed
[self displayTimestampForItemIfNeeded:item atIndex:indexPath.item];
//Update Top Message Padding Based On Who Sent The Previous Message
[self updateDistanceFromPreviousMessageForItem:item atIndex:indexPath.item];
//Display Delivery Status if Needed (e.g. Delivered)
[self displayDeliveryStatusForItemIfNeeded:item atIndex:indexPath.item];
//Set Message
[item setMessage:message];
}
私の 'cellForItemAtIndexpath'は正しく実装されていないのでしょうか?私の 'UICollectionViewCell'サブクラスはプログラム的に(ストーリーボードやxibファイルなしで)実行されています。私は初期ロード時にそれに気づいています。その' initWithFrame'メソッドはmy.messages配列の各項目に対して呼び出されています。これは正常ですか? – KingPolygon