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

时间:2022-02-23 12:49:21

My Table View not displaying data entered from UIAlert fields. I know for sure that the data is passed into [String]'s dedicated for this purpose, but nothing comes up. The only thing is, when I add new data, tableView cell separator disappears between 1st and 2nd Prototype cells, but nothing is displayed. Same happens further if I continue to add new data. What am I doing wrong? Heres the entire code, its only one class.

我的表视图不显示从UIAlert字段输入的数据。我确信数据被传递到专门用于此目的的[String]中,但没有任何结果。唯一的事情是,当我添加新数据时,tableView单元格分隔符在第一个和第二个Prototype单元格之间消失,但​​没有显示任何内容。如果我继续添加新数据,情况会进一步发生。我究竟做错了什么?继承了整个代码,它只有一个类。

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.
    }
}

1 个解决方案

#1


0  

You have a never ending loop of 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.

你有一个永无止境的reloadData()循环。如果使用当前实现在此方法中设置断点,您将注意到它是连续执行的。该单元格从未出现,因为它一直在尝试重新加载它。

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
}

#1


0  

You have a never ending loop of 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.

你有一个永无止境的reloadData()循环。如果使用当前实现在此方法中设置断点,您将注意到它是连续执行的。该单元格从未出现,因为它一直在尝试重新加载它。

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
}