6

HTML biçiminde yorumların listesini görüntüleyen dinamik boyutlandırma hücreleriyle birlikte UITableView'ım var ve NSAttributedString HTML içeriğini son derece yavaş bir şekilde oluşturması sorunuyla karşılaştım!NSAttributedString'de çok yavaş HTML oluşturma

İşte profil görüntüleyiciden.

enter image description here

Ayrı konuya NSAttributedString başlatma koymak için çalıştı, ancak HTML render edilirken ve nihayet hücrenin render düzgün düzeni değildir bittiğinde hala yavaş ve kullanıcı boş hücreleri görür.

dispatch_async(GlobalQueue, { 
     let html = NSAttributedString(
        data: self.comment.htmlBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false)!, 
        options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
        documentAttributes: nil, 
        error: nil) 

     dispatch_async(MainQueue, { 
      self.commentLabel.attributedText = html 
      self.commentLabel.font = UIFont(name: "HelveticaNeue-Light", size: 14.0)! 

      if let writer = self.comment.author { 
       self.authorLabel.text = writer.name 
      } 

      self.layoutIfNeeded() 
     }) 
    }) 

enter image description here

render hızlandırmak ve hücre düzenini nasıl düzeltileceği Lütfen tavsiye aşağıdaki görünüyor.

Teşekkürler!

Update:

hücre temsilci ve bayrak atfedilen dizisi başlatılır belirten ile çözüldü. Belki yardımcı olacağını birileri: bir dizeye HTML yavaş ayrıştırma hakkında

// TicketCell  
private var isContentInitialized = false 
private var commentAttributedString:NSAttributedString? 

var delegate: TicketCommentCellDelegate? 
var indexPath: NSIndexPath! 
var comment: TicketComment! { 
    willSet { 
     if newValue != self.comment { 
      self.isContentInitialized = false 
     } 
    } 
    didSet{ 
     self.configure() 
    } 
} 

... 
private func configure() {   
    if isContentInitialized { 
     // here might be activity indicator stop 
     ... 
     if let writer = self.comment.author { 
      self.authorLabel.text = writer.name 
     } 
    } 
    else { 
     // here might be activity indicator start 

     dispatch_async(GlobalQueue, { 
      self.commentAttributedString = NSAttributedString(
           data: self.comment.htmlBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false)!, 
           options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
           documentAttributes: nil, 
           error: nil)       

      self.isContentInitialized = true 

      // here might be spinner stop 
      dispatch_async(MainQueue, { 
       self.delegate?.ticketCommentCellDidRenderCommentHTML(self) 
      }) 
     }) 
    } 
} 

... 
protocol TicketCommentCellDelegate { 
    func ticketCommentCellDidRenderCommentHTML(cell: TicketCommentCell) 
} 


// TableViewDataSource 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(kTicketCommentCellIdentifier, forIndexPath: indexPath) as! TicketCommentCell 

    cell.indexPath = indexPath 
    cell.delegate = self 
    cell.comment = self.rows[indexPath.section][indexPath.row] 

    return cell 
} 

// MARK: - TicketCommentCellDelegate 

func ticketCommentCellDidRenderCommentHTML(cell: TicketCommentCell) { 
    self.tableView.reloadRowsAtIndexPaths([cell.indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
} 

// MARK: UITableViewDelegate 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {   
    var cell = self.commentCell 

    cell.comment = self.rows[indexPath.section][indexPath.row] 
    cell.setNeedsDisplay() 
    cell.setNeedsLayout() 

    let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 1 

    return height 
} 
+0

İçeriğiniz ne kadar büyük? – holex

+0

Büyük, basit bir e-posta şablonu, bir cümle, bir paragraf hakkında birkaç href bağlantısı ile imza. Yaklaşık 1000 karakter – Madman

+0

, başka bir kaynaktan başka _CSS_ veya _JavaScript_? – holex

cevap

9

: HTML bir atfedilen dize oluşturmak ilk kez iOS dizesini ayrıştırmak için gerekli ekstra iplikler her türlü oluşturur aralarında JavaScriptCore motor. HTML ilk NSAttributedString ayrıştırma önce

:

Before

Ve hemen sonra:

enter image description here

Yani bazen bu her başlatmak için neredeyse ikinci sürer tahmin edebilirsiniz. Sonraki aramalar çok daha hızlıdır.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    NSMutableAttributedString *attrStringFromHtml = [[NSMutableAttributedString alloc] 
                initWithData: [@"<span>html enabled</span>" dataUsingEncoding:NSUnicodeStringEncoding 
                       allowLossyConversion:NO] 
                options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} 
                documentAttributes:nil error:nil]; 
    NSLog(@"%@",[attrStringFromHtml string]); 

    return YES; 
} 

Ayrıca this answer bkz: My geçici çözüm (Objective-C) gerektiğinde ben bellekte gerekli tüm çerçeveler vardı ki, AppDelegate içinde application:didFinishingLaunchingWithOptions: fonksiyonunda HTML Ayrıştırma oldu.