ios - UutableView 选定的单元格到其他 View

标签 ios swift uitableview

我有一个 UITableView,其中数据是从数据库(JSON)加载的。当我选择在另一个 View 中拍摄的一行时,如何获得此信息?

自动标记将在表格 View 中被选择并显示在另一个 View 的标签中。

class AutoMarkeTableView: UITableViewController {
    var items = [[String:AnyObject]]()

    @IBOutlet var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "URL_LINK")!
        let urlSession = URLSession.shared

        let task = urlSession.dataTask(with: url) { (data, response, error) in
            // JSON parsen und Ergebnis in eine Liste von assoziativen Arrays wandeln
            let jsonData = try! JSONSerialization.jsonObject(with: data!, options: [])
            self.items = jsonData as! [[String:AnyObject]]

            // UI-Darstellung aktualisieren
            OperationQueue.main.addOperation {
                self.tableView.reloadData()
            }
        }

        task.resume()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "markeCell", for: indexPath)

        let item = items[indexPath.row]

        cell.textLabel?.text = item["makename"] as? String

        return cell
    }
}

class FahrzeugAngabenView: UIViewController {
    @IBOutlet weak var itemMarkeLabel: UILabel!
}

View1

View2

最佳答案

您可以将所选项目临时保存在变量中。像这样的事情:

var selectedItem: Item?
func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedItem = items[indexPath.row]
    self.performSegue(withIdentifier: "auto", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "auto" {
        let destVc = segue.destination  as! FahrzeugAngabenView

        destVc.selectedItemName = selectedItem.title
        selectedItem = nil
    }
 }

不是最优雅的解决方案,但我希望它能起作用。

关于ios - UutableView 选定的单元格到其他 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892964/

相关文章:

ios - 一次引用 2 个不同 IBOutlet 的最有效方法(其中每个 IBOutlet 仅针对其自己的大小类别存在)?

ios - 你还能用 swift 2 在 xcode 7 中编程吗?

IOS Swift - 如何在具有不同数据的 Collection View 中使用 2 个 TableView

ios - "CL Typing Label "swift 库错误

iOS:一段时间后保存托管对象上下文需要超过 1 秒

ios - "An App ID with bundle identifier X is not available. Please enter a different string"(Xcode 7.3)

javascript - 为什么我的 'position:fixed' 导航栏显示在网页底部而不是移动视口(viewport)底部?

android - 如何在flutter中实现实时人脸解锁功能

ios - 如何将底部边框添加到 TableView 部分标题

ios - UITableview 单元格未在 reloaddata 上更新