Как привязать словарь ссылок полученных через alertcontroller к изображению в ячейке? Swift
У меня есть словарь ссылок на картинки. Все сохраняется на диск. Сами ссылки получаю через alertController. Как можно подвязать картинку cell.imageView.image к словарю ссылок.
let currentImageItem = gameImageArray[indexPath.row]
cell.gameImage.image = currentImageItem["Name"] as? UIImage.
На текущий момент у меня есть такой код:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var i = UIImage(named: "exampleImage.png")
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gameNameArray.count
}
// MARK: - метод редактирования
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
// MARK: - метод условия удаления ячейки
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
removeGame(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
}
}
// MARK: - метод определяющий режим редактирования таблицы - запуск когда перетаскиваем одну ячейку на место другой
func tableView(_ tableView: UITableView, moveRowAt fromIndexpath: IndexPath, to: IndexPath) {
moveItem(fromIndex: fromIndexpath.row, toIndex: to.row) //метод описан в Model
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCellTableViewCell
let currentNameItem = gameNameArray[indexPath.row]
cell.gameNameLabel?.text = currentNameItem["Name"] as? String
let currentSubNameItem = gameSubNameArray[indexPath.row]
cell.subGameNameLabel?.text = currentSubNameItem["Name"] as? String
let currentScoreItem = gameScoreArray[indexPath.row]
cell.gameScoreValue?.text = currentScoreItem["Name"] as? String
let currentImageItem = gameImageArray[indexPath.row]
cell.gameImage.image = currentImageItem["Name"] as? UIImage
//cell.gameImage.image = i
// MARK: - привязываем функционал кнопки
//cell.addGameImageButton.addTarget(self, action: #selector(addImage(sender:)), for: .touchUpInside)
return cell
}
@IBAction func addNewGameCell(_ sender: Any) {
let alertController = UIAlertController(title: "Create new game", message: "Message", preferredStyle: .alert)
alertController.addTextField { (textFiled) in
textFiled.placeholder = "Enter name"
}
alertController.addTextField { (textFiled) in
textFiled.placeholder = "Enter info"
}
alertController.addTextField { (textFiled) in
textFiled.placeholder = "Enter game score"
}
alertController.addTextField { (textFiled) in
textFiled.placeholder = "Enter URL image"
}
let alertAction1 = UIAlertAction(title: "Cancel", style: .default) { (alert) in
}
let alertAction2 = UIAlertAction(title: "Create", style: .cancel) { [self] (alert) in
let newNameItem = alertController.textFields![0].text
addNameGame(nameItem: newNameItem!)
let newSubNameItem = alertController.textFields![1].text
addSubNameGame(subNameItem: newSubNameItem!)
let newScoreItem = alertController.textFields![2].text
addScoreGame(scoreItem: newScoreItem!)
let newImageItem = alertController.textFields![3].text
addImageGame(imageItem: newImageItem!)
//let data = NSData(contentsOf: NSURL(string: newImageItem!)! as URL)
//i = UIImage(data: data! as Data)
self.tableView.reloadData()
}
alertController.addAction(alertAction1)
alertController.addAction(alertAction2)
present(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140.0
}
@IBAction func editTableAction(_ sender: Any) {
tableView.setEditing(!tableView.isEditing, animated: true)
}
}