我有主View Controller,应用程序的数据在哪里。这些数据,我需要发送到包含UIView并嵌套到他的UITableView的视图中。 UITableView负责显示弹出菜单,其中我的部分数据位于单元格中(特别是主题的标题)。
我尝试通过协议和委托来执行此操作,但未成功。也许我做错了什么,或者总的来说是不可能的,我需要寻找另一种出路?
要传递的数据
fileprivate var themes = [
Theme(name: "Halloween",
emoji: ["", "", "", "☠️", "", "", "⚰️", "", "", "", "⚱️", "", "", "", "", "", "", ""],
backgroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),
buttonColor: #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)),
Theme(name: "Sports",
emoji: ["", "⚽️", "", "", "", "♂️", "♀️", "⛳️", "", "", "", "⛸", "", "", "", "", "♂️", "♂️"],
backgroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),
buttonColor: #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)),
Theme(name: "Flags",
emoji: ["", "", "", "️", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
backgroundColor: #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1),
buttonColor: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
Theme(name: "Animals",
emoji: ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
backgroundColor: #colorLiteral(red: 0.3098039329, green: 0.2039215714, blue: 0.03921568766, alpha: 1),
buttonColor: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
Theme(name: "Food",
emoji: ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
backgroundColor: #colorLiteral(red: 0.1294117719, green: 0.2156862766, blue: 0.06666667014, alpha: 1),
buttonColor: #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)),
Theme(name: "Gadgets",
emoji: ["⌚️", "", "", "", "", "", "", "", "", "", "", "", "⏰", "", "", "⏲", "⌨️", ""],
backgroundColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1),
buttonColor: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1))
]
视图类
class DropDownView: UIView {
fileprivate var listOfThemes = ["1", "2", "3"] //test data
fileprivate struct Keys {
static let cellIdentifier = "cell"
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} }
extension DropDownView: UITableViewDelegate, UITableViewDataSource {
fileprivate func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfThemes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Keys.cellIdentifier, for: indexPath)
cell.textLabel?.text = listOfThemes[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
} }
答案 0 :(得分:1)
有几种方法可以实现此目的,但在这种情况下,以下内容似乎很容易实现:
在您的DropDownView
类中,声明listOfThemes
数组,如下所示:
var listOfThemes: [Theme]!
然后在您的mainVC中初始化DropDownView时,在显示视图之前执行以下操作:
fileprivate var dropDownView = DropDownView()
dropDownView.listOfThemes = themes // themes are the themes from your main VC
// now present it.