在iOS中的UITableViewCell上呈现HTML

时间:2018-12-22 11:31:07

标签: html ios swift rendering

我需要渲染String[] images = (String[])a.getArray();,该图像可以在// my PostDAO class public static void closeConn() throws SQLException { conn.close(); } // another class public void anotherClassFunction(){ ... String[] images = (String[])a.getArray(); PostDAO.closeConn(); } 的不同单元格上具有图像和文本。我的数据源是HTML内容,很可能需要在HTML上呈现。

我也尝试过在UITableViewUITableViewCell上使用属性字符串,但是它们在滚动UILabel时也不起作用 我尝试了此方法,但无法获得有关不同类型电池的帮助。

我也尝试过https://github.com/Vugla/PSHTMLView,但这有一些问题,例如,它仅有助于使相似的单元出队(我需要使用具有不同html的单元)

任何帮助我将不胜感激。 指针也会有所帮助:)

2 个答案:

答案 0 :(得分:0)

Add these constraints with height向您可以使用UITextView添加constarint只需添加一个约束,即高度大于等于属性的高度约束,并将常数更改为999,并在单元格中使用以下代码

self.heightUITextView.constant = self.newStoryTextView.contentSize.height/2

这里heightUITextView是我的身高限制,newStoryTextView是我的UITextView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! YourTableViewCell

            cell.heightUITextView.constant = self.newStoryTextView.contentSize.height/2
            cell.view.setNeedsLayout()
            return cell
    }

您的TableCell:

class YourTableViewCell : UITableViewCell {

    @IBOutlet weak var viewHeight: NSLayoutConstraint!

}

答案 1 :(得分:0)

我曾经遇到过类似的用例,总是很痛苦。因此,我主要考虑以下两种选择:

  1. 使用可用的本机API。

您将HTML转换为这样的属性字符串(请参见Convert HTML to NSAttributedString in iOS):

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
    NSAttributedString.DocumentType.html]
let attributedString = try? NSAttributedString(data: htmlData ?? Data(),
                                                      options: options,
                                                      documentAttributes: nil)

但是,这很慢,并且与UITableViewCell结合使用时,可能会导致崩溃。我想这是因为本地HTML解析器必须加载WebKit并在后台线程或队列上解析HTML,这与UITableViewCell呈现管道效果不佳。因此,如果您走这条路,我建议预处理HTML(并将其缓存),并让UITableViewCell使用属性字符串作为数据源。

  1. 使用第三方库。

DTCoreText(https://github.com/Cocoanetics/DTCoreText)似乎可以解决它,但是您应该注意许可。

如果您只希望剥离HTML标签,则可以使用Google https://chromium.googlesource.com/external/github.com/google/google-toolbox-for-mac/+/refs/heads/master/Foundation/GTMNSString+HTML.h的扩展名