简体   繁体   English

表格视图不显示从UIAlert字段输入的数据

[英]Table View not displaying data entered from UIAlert fields

My Table View not displaying data entered from UIAlert fields. 我的表格视图未显示从UIAlert字段输入的数据。 I know for sure that the data is passed into [String]'s dedicated for this purpose, but nothing comes up. 我确定将数据传递到为此目的专用的[String]中,但是什么也没有发生。 The only thing is, when I add new data, tableView cell separator disappears between 1st and 2nd Prototype cells, but nothing is displayed. 唯一的事情是,当我添加新数据时,tableView单元格分隔符在第一个和第二个Prototype单元格之间消失,但​​是什么也不显示。 Same happens further if I continue to add new data. 如果我继续添加新数据,还会发生同样的情况。 What am I doing wrong? 我究竟做错了什么? Heres the entire code, its only one class. 这是整个代码,只有一个类。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!

    var textFields = [UITextField?]()

    var entries = [String]()
    var usernames = [String]()
    var passwords = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Password Vault"
        navigationItem.prompt = "Add entry by clicking on the '+' sign -->"
        tableView.delegate = self
        tableView.dataSource = self

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addEntry")
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "edit")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return entries.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = entries[indexPath.row]
        cell.accessoryType = .DetailDisclosureButton
        cell.selectionStyle = .Blue
        tableView.reloadData()

        return cell
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    }

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

        return true
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            entries.removeAtIndex(indexPath.row)
            tableView.reloadData()
        }
    }

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {

        let ac = UIAlertController(title: "Details for \(self.entries[indexPath.row])", message: nil, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "username: \(self.usernames[indexPath.row]) ", style: .Default, handler: nil))
        ac.addAction(UIAlertAction(title: "password: \(self.passwords[indexPath.row])", style: .Default, handler: nil))
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))

        presentViewController(ac, animated: true, completion: nil)
    }

    func entry(textField: UITextField!) -> Void {
        textField.placeholder = "enter entry name"
        textFields.append(textField)

    }

    func username(textField: UITextField!) ->Void {
        textField.placeholder = "enter username"
        textFields.append(textField)

    }

    func password(textField: UITextField!) -> Void {
        textField.placeholder = "enter password"
        textFields.append(textField)
    }

    func addEntry() {
        let ac = UIAlertController(title: "New Entry", message: "Enter entry name, username and password", preferredStyle: .Alert)
        ac.addTextFieldWithConfigurationHandler(entry)
        ac.addTextFieldWithConfigurationHandler(username)
        ac.addTextFieldWithConfigurationHandler(password)
        ac.addAction(UIAlertAction(title: "OK", style: .Default) {[unowned self] _ in
            self.entries.append(self.textFields[0]!.text!)
            self.usernames.append(self.textFields[1]!.text!)
            self.passwords.append(self.textFields[2]!.text!)
            self.tableView.reloadData()
            self.textFields.removeAll()
        })

        ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:nil))

        presentViewController(ac, animated: true, completion: nil)

        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "edit")
    }

    func edit() {
        tableView.editing = true
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "doneEditing")

    }

    func doneEditing() {
        tableView.editing = false
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "edit")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

You have a never ending loop of reloadData() . 您有一个无休止的reloadData()循环。 If you set a breakpoint in this method with your current implementation, you will notice that it's continuously executed. 如果使用当前的实现在此方法中设置一个断点,则会注意到它是连续执行的。 The cell never appeared because it was constantly trying to reload it. 该单元从未出现,因为它一直在尝试重新加载它。

Your method cell for index path should look like this 您的索引路径的方法单元格应如下所示

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.textLabel?.text = entries[indexPath.row]
    cell.accessoryType = .DetailDisclosureButton
    cell.selectionStyle = .Blue
    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM