iTextSharp Table:我需要调暗任何单元格或列还是可以重复使用它们?

iTextSharp Table: do I need to DIM any cell or column or can I reuse them?

我有一个 table 的 3 列

当我在 Dim

之前为任何单元格进行变量分配时,此代码有效
text1 = field1
Dim cell1 As New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell1)

text2 = field2
Dim cell2 As New PdfPCell(New Phrase(text2, myFont))
table.AddCell(cell2)

text3 = field3
Dim cell3 As New PdfPCell(New Phrase(text3, myFont))
table.AddCell(cell3)

但为什么像这样的代码不起作用?

text1 = field1
Dim cell1 As New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell1)

text1 = field2
table.AddCell(cell2)

text1 = field3
table.AddCell(cell3)

我真的需要一个一个地定义或调暗任何单元格或列吗?

每个单元格需要一个新实例,但不必创建新变量。您可以重复使用变量名。只需在需要时分配一个新实例。

Dim cell As PdfPCell

text1 = field1
cell = New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell)

text2 = field2
cell = New PdfPCell(New Phrase(text2, myFont))
table.AddCell(cell)

text3 = field3
cell = New PdfPCell(New Phrase(text3, myFont))
table.AddCell(cell)