问题描述
我一直在使用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
请您提供帮助。
解决方法
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。
最好的问候, 菲尔(TPPDF的创建者)