我有两个UITableViewControllers,它们都包含非常相似的单元格(每个单元格类有2个UIImageViews和2个UILabels)。第一个视图控制器是用Objective-C编写的,纯粹用代码实现。第二个视图控制器是用Swift编写的,并且在故事板中进行了布局。
我的问题是第一个视图控制器在滚动到顶部时抖动,而第二个视图控制器在滚动到顶部时非常平滑。这是video。
第一个TableViewController的cellForRowAtIndexPath :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LYRConversation *conversation = _searching && _filteredDataSource.count ? [_filteredDataSource objectAtIndex:indexPath.row] : [_dataSource objectAtIndex:indexPath.row];
if (conversation.participants.count > 2) {
ConversationListGroupCell *cell = (ConversationListGroupCell *)[tableView dequeueReusableCellWithIdentifier:GROUP_CELL_REUSE_ID forIndexPath:indexPath];
[self configureGroupCell:cell atIndexPath:indexPath conversation:conversation];
return cell;
}
ConversationListCell *cell = (ConversationListCell *)[tableView dequeueReusableCellWithIdentifier:CELL_REUSE_ID forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath conversation:conversation];
return cell;
}
-(void)configureCell:(ConversationListCell *)conversationCell atIndexPath:(NSIndexPath *)indexPath conversation:(LYRConversation *)conversation
{
[conversationCell updateAvatarWithUrl:[NSURL URLWithString:[self avatarStringUrlForConversation:conversation]]];
[conversationCell updateTitle:[self titleForConversation:conversation]];
[conversationCell updateUnreadMessageIndicator:conversation];
NSArray *otherParticipants = [[PersistentCache sharedCache] cachedUsersForUserIDs:conversation.participants];
conversationCell.preview.text = [((PFUser *)otherParticipants.lastObject) objectForKey:@"jobTitle"];
[UIView updateStatusIndicatorOfConversation:otherParticipants statusIndicator:conversationCell.status];
}
-(void)configureGroupCell:(ConversationListGroupCell *)conversationGroupCell atIndexPath:(NSIndexPath *)indexPath conversation:(LYRConversation *)conversation {
NSArray *urls = [self avatarStringUrlsForConversation:conversation];
if (urls.count == conversation.participants.count - 1) {
NSURL *url1 = [NSURL URLWithString:urls[0]];
if(url1 != NULL) {
downloadImageThenSetForImageView(conversationGroupCell.avatar1, url1);
}
NSURL *url2 = [NSURL URLWithString:urls[1]];
if (url2 != NULL) {
downloadImageThenSetForImageView(conversationGroupCell.avatar2, url2);
}
if (urls.count > 2) {
NSURL *url3 = [NSURL URLWithString:urls[2]];
if (url3 != NULL) {
downloadImageThenSetForImageView(conversationGroupCell.avatar3, url3);
}
conversationGroupCell.avatar3.hidden = NO;
}
if (urls.count > 3){
conversationGroupCell.plusOne.hidden = NO;
conversationGroupCell.plusOne.text = [NSString stringWithFormat:@"+%lu", urls.count - 3];
}
}
conversationGroupCell.title.text = [self titleForConversation:conversation];
if (conversation.lastMessage.isUnread) {
//[self.unreadMessageIndicator setImage:[ConversationListCell unreadBlue] forState:UIControlStateNormal];
[conversationGroupCell.unreadMessageIcon setImage:[Assets unreadBlue]];
}
else {
//[self.unreadMessageIndicator setImage:[ConversationListCell unreadGray] forState:UIControlStateNormal];
[conversationGroupCell.unreadMessageIcon setImage:[Assets unreadGray]];
}
NSString *idOfLastSender = conversation.lastMessage.sender.userID;
NSString *lastMessageText = NULL;
if ([idOfLastSender isEqualToString:self.layerClient.authenticatedUserID]) {
lastMessageText = @"You";
}
else {
lastMessageText = [[PersistentCache sharedCache] resolvedFullNamesFromParticipants:@[idOfLastSender]].firstObject;
}
conversationGroupCell.preview.text = [NSString stringWithFormat:@"%@
sent last message", lastMessageText];
}
第二个TableViewController的cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CallLogCell
{
guard let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifiers.PhoneCell.rawValue, forIndexPath: indexPath) as? CallLogCell else {
return CallLogCell()
}
configureCell(cell, indexPath: indexPath)
return cell
}
func configureCell(cell: CallLogCell, indexPath: NSIndexPath) {
let callParticipantInfo = CallLog.callLogDataSource[indexPath.row]
// TODO: Add all these to the cell class as methods
// TODO: Make sure UI looks okay if any of this stuff is nil
// Set Name
guard let callParticipant = callParticipantInfo[CallLogDataSourceIndices.Object] as? PFUser else {
return
}
cell.nameLabel.text = callParticipant.fullName()
// Set Image
let imageUrlString = callParticipant[ParseKeys.ImageUrl.rawValue] as? String ?? ""
if let imageUrl = NSURL(string: imageUrlString) {
downloadImageThenSetForImageView(cell.picture, imageUrl)
}
else {
cell.picture.image = Assets.placeholder()
}
// Set Status
let status = callParticipant[ParseKeys.Status.rawValue] as? NSNumber ?? NSNumber.statusCodeUnknown()
cell.statusIndicator.backgroundColor = UIColor.PNGStatusColorForStatusCode(status.integerValue)
// Set Missed call
let missedCall = callParticipantInfo[CallLogDataSourceIndices.Missed] as? String ?? ""
if missedCall != "" && Int(missedCall) != nil {
cell.updateMissedCallIndicator(Int(missedCall)!)
}
// Set Job Title
let jobTitle = callParticipant[ParseKeys.JobTitle.rawValue]
cell.jobTitle.text = jobTitle as? String
// Set Date
if let callDate = callParticipantInfo[CallLogDataSourceIndices.Date] as? String {
cell.date.text = formatDate(callDate)
}
}
两个视图控制器使用的方法
void downloadImageThenSetForImageView(UIImageView * imageView, NSURL *url) {
[[SDWebImageManager sharedManager] downloadImageWithURL:url options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
imageView.image = [UIImage resize:image newSize:imageView.bounds.size];
}];
}
extension UIImage {
class func resize(bigImage: UIImage, newSize: CGSize) -> UIImage {
let hasAlpha = false
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(newSize, !hasAlpha, scale)
bigImage.drawInRect(CGRect(origin: CGPointZero, size: newSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
SDWebImage:https://github.com/rs/SDWebImage - 根据文档,图片下载器是异步的。
乐器的屏幕截图'时间分析器分析
我在滞后视图控制器上滚动几次后拍摄了这个截图。如果我隐藏系统库,那么它没有显示任何让我相信我的代码没有瓶颈的东西?