Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试在UITableView中插入新行时出现格式问题_Ios_Swift_Uitableview - Fatal编程技术网

Ios 尝试在UITableView中插入新行时出现格式问题

Ios 尝试在UITableView中插入新行时出现格式问题,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试使用alertViewController获取文本,将其添加到字符串数组中,然后使用新添加的单元格重新加载tableView。重新加载后,格式似乎有问题 import UIKit class TableViewController: UITableViewController, UINavigationControllerDelegate { // store the tasks in an array of strings var tasks = [String](

我正在尝试使用alertViewController获取文本,将其添加到字符串数组中,然后使用新添加的单元格重新加载tableView。重新加载后,格式似乎有问题

import UIKit

class TableViewController: UITableViewController, UINavigationControllerDelegate {
    // store the tasks in an array of strings
    var tasks = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Task List"
        self.navigationItem.rightBarButtonItem = self.editButtonItem
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addRow))
    }

    func addRow() {
        let ac = UIAlertController(title: "Add a task to the list", message: nil, preferredStyle: .alert)

        // add a text field
        ac.addTextField {
            (textField) -> Void in
            textField.placeholder = ""
        }

        // add "cancel" and "ok" actions
        ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        let createNewRow = UIAlertAction(title: "OK", style: .default) { action -> Void in
            let text = ac.textFields?.first?.text
            self.tasks.append(text!)
            self.loadView()
        }
        ac.addAction(createNewRow)

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

    }
    // MARK: - Table view data source

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

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

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

        cell.textLabel?.text = tasks[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            tasks.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
}

您的问题是在警报视图控制器的操作中调用self.loadView()而不是self.tableView.reloadData():

    let createNewRow = UIAlertAction(title: "OK", style: .default) { action -> Void in
        let text = ac.textFields?.first?.text
        self.tasks.append(text!)
        self.tableView.reloadData() // self.loadView() is wrong.
    }
    ac.addAction(createNewRow)
来自苹果公司的文件:

永远不要直接调用此方法。视图控制器调用 此方法在请求其view属性但当前为nil时使用。 此方法加载或创建视图并将其指定给视图 财产