【问题标题】:TPPDF Table Data Version 2.3TPPDF 表格数据版本 2.3
【发布时间】:2021-01-15 12:34:11
【问题描述】:

我一直在使用 TPPDF 来利用表格生成 pdf。 使用旧版本,我可以按照下面的代码填充、设置对齐方式和样式。

_bodyData [Array] 由 [String, String, UIImage as Any, String, UIImage as Any] 组成

static func addTextTableBody(_bodyData: [Array<Any>], _bodyAlign: [Array<PDFTableCellAlignment>], _bodyWidth: Array<CGFloat>, _bodyStyle: [Array<PDFTableCellStyle>], _colHeaderCount: Int, _colHeaderStyle: PDFTableCellStyle, _rowHeaderStyle: PDFTableCellStyle, _contentStyle: PDFTableCellStyle, _altContentStyle: PDFTableCellStyle, _outlineStyle: PDFLineStyle, _padding: CGFloat, formType: Int) ->PDFTable {
    let tableData = PDFTable()
    // Tables can contain Strings, Numbers, Images or nil, in case you need an empty cell. If you add a unknown content type, an error will be thrown and the rendering will stop.
    do {
        try tableData.generateCells(
        data: _bodyData,
        alignments: _bodyAlign)
    } catch PDFError.tableContentInvalid(let value) {
        // In case invalid input is provided, this error will be thrown.
        print("This type of object is not supported as table content: " + String(describing: (type(of: value))))
    } catch {
        // General error handling in case something goes wrong.
        print("Error while creating table: " + error.localizedDescription)
    }

    // The widths of each column is proportional to the total width, set by a value between 0.0 and 1.0, representing percentage.
    tableData.widths = _bodyWidth

    // To speed up table styling, use a default and change it
    let style = PDFTableStyleDefaults.simple
    style.columnHeaderStyle = _colHeaderStyle
    style.rowHeaderStyle = _colHeaderStyle

    style.contentStyle = _colHeaderStyle
    style.alternatingContentStyle = _colHeaderStyle

    style.columnHeaderCount = _colHeaderCount
    style.rowHeaderCount = 0
    style.footerCount = 0

    style.outline = _outlineStyle//PDFLineStyle(type: .full)
    tableData.style = style

    // Set table padding and margin
    tableData.padding = _padding
    tableData.margin = 0.0

    return tableData
}

在新版本中,我看到过这样的例子TPPDF Table Alignments 当表格内容接受以下形式的数据时,如何将字符串、整数、图像混合到表格数据中。

let tableData: [[String]] = [...] // This is your nested data

请在这方面提供任何帮助。

【问题讨论】:

  • 嗨 thomkas,您的 bodyData[[Any]] 类型。应该没有什么可以阻止您按照您在运行let alignments = bodyData.map { $0.map { _ in PDFTableCellAlignment.right }} 中发布的链接中的示例代码进行操作。如果您的问题是关于数组的数据类型 ([[Any]]),那么可能值得关注 github.com/techprimate/TPPDF/blob/master/Documentation/…。您可能需要将元素包装在 PDFTableCellContent 包装器中。
  • 感谢您的回复。我是 Swift 的新手,而包装器是我一直在努力解决的问题。您对如何处理上述数据类型有任何指示吗?
  • 我从来没有使用过或听说过这个库,所以我不能从经验中说出来,但是看看github.com/techprimate/TPPDF/blob/master/Source/API/Table/…,你似乎可以打电话给let cellContent = PDFTableContent(content: whatever)。如果您需要访问该单元格的类型,您可以检查cellContent.type,它在内部设置为您在init 中传递的内容类型。如果您提供tableData.generateCells 的实现,这可能会帮助人们回答您的问题。或者,也许您可​​以将问题改写得更具体

标签: swift tppdf


【解决方案1】:

generateCells() 的 API 已被弃用一段时间,并在最近的更新中被删除。

请查看Table Example,它展示了如何使用更高级的 API。这里摘录:

var table = PDFTable(rows: 3, columns: 2)
table.content = [
    nil, "Name",
    1,   "Waterfall",
    2,   "Fireworks"
]
table.rows.allRowsAlignment = [.center, .left]
table.widths = [
   0.2, 0.8
]

我希望这会有所帮助,否则请仔细查看 documentation

最好的问候, Phil(TPPDF 的创建者)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2012-04-21
    • 2011-10-18
    • 2021-03-29
    相关资源
    最近更新 更多