[英]Table View not displaying data entered from UIAlert fields
我的表格視圖未顯示從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.
}
}
您有一個永無休止的reloadData()循環。 如果使用當前的實現在此方法中設置一個斷點,則會注意到它是連續執行的。 該單元從未出現,因為它一直在嘗試重新加載它。
您的索引路徑的方法單元格應如下所示
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.