【发布时间】:2016-03-22 03:09:04
【问题描述】:
我的 collectionView 中有大约 14 个项目,滚动一点也不流畅。我正在使用自动布局(我的约束不记录错误),并且我正在使用cornerRadius 属性(它是一个消息传递应用程序)。
我尝试使用 Time Profiler 工具对其进行分析,这是我在滚动时看到的:
不幸的是,“Reveal in 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 数组中的每个项目调用。这正常吗?
标签: ios objective-c xcode uicollectionview smooth-scrolling