Swift Playgrounds中的简单笔记应用

时间:2017-10-28 07:14:58

标签: ios swift uitableview uitextview swift-playground

我正在尝试在Swift Playgrounds中创建一个简单的笔记应用程序,但我被卡住了。

如何将主视图(UITableView)中的数据发送到详细视图(UITextView),反之亦然,而不是prepareForSegue,因为没有故事板?< / p>

我错过了什么或做错了什么?

我一直在努力寻找解决方案,所以任何帮助都会受到高度赞赏。

这是我的游乐场:

import UIKit
import PlaygroundSupport

class MasterViewController: UITableViewController {

    var noteData: [String] = []
    var selectedRow: Int = -1
    var newRowLabel: String = ""

    override func viewDidLoad() {
        configureNavigationBar()
        loadNote()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if selectedRow == -1 {
            return
        }
        noteData[selectedRow] = newRowLabel
        if newRowLabel == "" {
            noteData.remove(at: selectedRow)
        }
        tableView.reloadData()
        saveNote()
    }

    @objc func newNote() {
        let noteTitle: String = ""
        noteData.insert(noteTitle, at: 0)
        let indexPath: IndexPath = IndexPath(row: 0, section: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
        navigationController?.pushViewController(DetailViewController(), animated: true)
    }

    func configureNavigationBar() {
        title = "Notes"
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(newNote))
        navigationItem.rightBarButtonItem = addButton
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        }
        cell?.textLabel?.text = noteData[indexPath.row]
        return cell!
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        noteData.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        saveNote()
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        navigationController?.pushViewController(DetailViewController(), animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let detailView: DetailViewController = segue.destination as! DetailViewController
        selectedRow = tableView.indexPathForSelectedRow!.row
        detailView.masterView = self
        detailView.setText(noteText: noteData[selectedRow])
    }

    func saveNote() {
        UserDefaults.standard.set(noteData, forKey: "Notes")
        UserDefaults.standard.synchronize()
    }

    func loadNote() {
        if let loadedNote = UserDefaults.standard.value(forKey: "notes") as? [String] {
            noteData = loadedNote
            tableView.reloadData()
        }
    }
}

class DetailViewController: UIViewController {

    let textView = UITextView()
    var noteText: String = ""
    var masterView: MasterViewController!

    override func loadView() {
        configureDetailView()
    }

    override func viewDidLoad() {
        configureTextView()
    }

    func configureDetailView() {
        view = textView
    }

    func configureTextView() {
        textView.text = noteText
    }

    func setText(noteText: String) {
        if isViewLoaded {
            textView.text = noteText
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        masterView?.newRowLabel = noteText
    }
}

let masterViewController = MasterViewController()
let navigationController = UINavigationController(rootViewController: masterViewController)

PlaygroundPage.current.liveView = navigationController

2 个答案:

答案 0 :(得分:2)

didSelectRow方法中:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    navigationController?.pushViewController(DetailViewController(), animated: true)
}

当你的视图控制器有变量来接受数据时,你正在创建你的DetailViewController()但是没有传递任何数据,所以为了做到这一点而不是这样做:

navigationController?.pushViewController(DetailViewController(), animated: true)

你可以这样做:

let detailVC = DetailViewController()
    detailVC.noteText = noteData[indexPath.row]
    navigationController?.pushViewController(detailVC, animated: true)

编辑:我正在从这里更新我的答案,因为您的解决方案需要进行一系列更改来制作内容,就像我上面介绍的如何传递数据的方式,您必须在各种方式执行此操作你正在创建新笔记的地方也是

首先要保存新内容并使用表格查看要更新的单元格,您需要调整newNote()函数:

@objc func newNote() {
    let noteTitle: String = ""
    noteData.insert(noteTitle, at: 0)
    let indexPath: IndexPath = IndexPath(row: 0, section: 0)
    tableView.insertRows(at: [indexPath], with: .automatic)
    tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    selectedRow = indexPath.row
    let detailVC = DetailViewController()
    detailVC.masterView = self
    navigationController?.pushViewController(detailVC, animated: true)
}

您要将selectedRow变量设置为已插入的indexPath,并将masterView传递给DetailsViewController()

其次,您还需要更改didSelectRow,使用我发布的相同解决方案,但同样也会传递masterViewselectedRow,所以它应该如下所示:< / p>

let detailVC = DetailViewController()
    detailVC.noteText = noteData[indexPath.row]
    detailVC.masterView = self
    selectedRow = indexPath.row
    navigationController?.pushViewController(detailVC, animated: true)

你的DetailViewController缺少一件事,只要在文本视图中输入文本它没有更新noteText并且您的viewWillDisappear方法依赖于此,您需要将文本视图的委托设置为自己并听取文字更改,但为了更简单的解决方案,请将viewWillDisappear更改为直接访问textView,如下所示:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    masterView?.newRowLabel = textView.text
}

第四,当您从此屏幕返回时,您的MasterViewController需要将此保存在viewWillAppear方法中,此处也需要进行一些更改,实际上您只需要重置selectedRow变量一旦注释被保存,所以在你打电话之后:

saveNote()

在下一行还添加:

selectedRow = -1

答案 1 :(得分:1)

而不是

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    navigationController?.pushViewController(DetailViewController(), animated: true)
}

你可以创建DetailViewController并分配noteText,就像这样

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let detailViewController = DetailViewController()
    detailViewController.noteText = noteData[indexPath.row]
    detailViewController.masterView = self
    navigationController?.pushViewController(detailViewController, animated: true)
}

可以删除功能prepare,因为您不使用故事板,所以不会使用该功能。

不要忘记改编newNote()

@objc func newNote() {
    let noteTitle: String = ""
    noteData.insert(noteTitle, at: 0)
    let indexPath: IndexPath = IndexPath(row: 0, section: 0)
    tableView.insertRows(at: [indexPath], with: .automatic)
    tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    let detailViewController = DetailViewController()
    detailViewController.masterView = self
    navigationController?.pushViewController(detailViewController, animated: true)
}