У меня около 14 элементов в моей коллекцииПросмотр и прокрутка не совсем гладкая. Я использую автоматический макет (мои ограничения не регистрируют ошибки), и я использую свойство cornerRadius (это приложение для обмена сообщениями).UICollectionView Прокрутка Производительность Ужасно
Я попытался проанализировать его с помощью инструмента Time Profiler, и это было то, что я вижу, когда я прокручиваю:
К сожалению, «Reveal в 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 вызывается для каждого элемента в моем массиве self.messages. Это нормально? – KingPolygon