简体   繁体   中英

Table View not displaying data entered from UIAlert fields

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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