Передать данные из View Controller в View - PullRequest
0 голосов
/ 21 января 2019

У меня есть главный 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)

} }

1 Ответ

0 голосов
/ 21 января 2019

Есть несколько способов достижения этого, но в этом случае следующее кажется простым и легким для достижения:

В вашем классе 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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...