我有这个结构(在单独的swift文件中):
struct Section {
var infoType: String!
var info: [String]!
var icon: [UIImage]!
var expanded: Bool!
init(infoType: String, info: [String],icon: [UIImage], expanded: Bool) {
self.infoType = genre
self.info = movies
self.icon = icon
self.expanded = expanded
}
}
我用它来填充tableView
在`viewController中,我这样做:
class UserInTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var userName: String = ""
@IBOutlet weak var tableView: UITableView!
var ref: DatabaseReference?
let user = Auth.auth().currentUser!.uid
var databaseHandle: DatabaseHandle?
var sections = [
Section(infoType: "Venue Info": ,
info: [], //I want to populate this array with string variables
icon: [],
expanded: false),
Section(infoType: "User Info": ,
info: [], //I want to populate this array with string variables
icon: [],
expanded: false),
Section(infoType: "Other Info": ,
info: [], //I want to populate this array with string variables
icon: [],
expanded: false)
]
如何用变量填充?
答案 0 :(得分:0)
您可以通过枚举数组来做到这一点
首先,按照Vadian的建议,将其更改为非可选,并且不要将图像直接保存到数组中,而是保存图像的名称。
struct Section {
var infoType: String
var info: [String]
var icon: [UIImage]
var expanded: Bool
}
在viewController中创建一个函数,只要拥有它,它就会将内容更改为实际内容。
func populateArrayWithActualInfo() {
for (index,section) in self.sections.enumerated() {
var secInside = section
secInside.info = ["a", "b", "c"]
self.sections[index] = secInside
}
print(self.sections)
}
或如果要从现有数组中设置动态信息,则可以将其传递给函数。
func populateArrayWithActualInfo(_ yourData:[[String]]) {
for (index,section) in self.sections.enumerated() {
var secInside = section
secInside.info = yourData[index]
self.sections[index] = secInside
}
print(self.sections)
}
尝试一下并分享结果。