【问题标题】:Trouble with text field entry to a table输入表格的文本字段出现问题
【发布时间】:2015-11-17 03:05:09
【问题描述】:

我的主要问题是我没有将我输入的文本输入到我制作的标签或表格中(有点偏差,但想稍微自定义一下)。我没有收到任何错误,但有人可以看看这段代码,看看其中是否有我可能忽略的重大错误吗? 编辑:我不断返回并更改类型之类的小东西,但没有任何效果。这是我的整个代码,以防它对每个人都更有意义

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate {

var weatherList = [String] ()

@IBOutlet weak var textField: UITextField!

@IBOutlet weak var textLabel: UILabel!

@IBAction func searchButton(sender: AnyObject) {

    weatherList.append(textField.text!)

    textField.text = ""

    NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

}

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if NSUserDefaults.standardUserDefaults().objectForKey("weatherList") != nil {

        weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList") as! [String]

    }

    self.textField.delegate = self

}

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

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

    return weatherList.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = weatherList[indexPath.row] as String

    return cell

}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == UITableViewCellEditingStyle.Delete {

        weatherList.removeAtIndex(indexPath.row)

        NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

        tableView.reloadData()

    }

}

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.view.endEditing(true)

}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    performAction()

    return true

}

func performAction() {

    weatherList.append(textField.text!)

    textField.text = ""

    NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

}

}

【问题讨论】:

    标签: ios swift uitableview uitextfield


    【解决方案1】:

    来自NSUserDefaults class reference

    NSUserDefaults 类提供了方便的访问方法 常见类型,例如浮点数、双精度数、整数、布尔值和 URL。一种 默认对象必须是一个属性列表,即(或 对于集合实例的组合):NSData,NSString, NSNumber、NSDate、NSArray 或 NSDictionary。如果你想存储任何 其他类型的对象,您通常应该将其归档以创建一个 NSData 的实例。

    这不是快速数组。改用 NSMutableArray:

    var weatherList:NSMutableArray = NSMutableArray()
    

    然后

    self.weatherList.addObject(textField.text)
    

    【讨论】:

    • 我解决了这个问题,但现在我在第一行的 AppDelegate 中遇到错误 |类 AppDelegate: UIResponder, UIApplicationDelegate { |说线程 1:信号 SIGABRT
    • 在您的代码中,您仍在使用 swift 样式数组。 NSUserDefaults 只能接受上面列出的对象类型。你也不能这样投:weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList") as! [String] 如果要使用 NSUserDefaults,则需要使用 NSArray。这是另一个问题,我给出了相同的问题和相同的答案:stackoverflow.com/questions/32798887/…
    • 好吧,如果我把它改成一个数组,我该怎么改 NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList") 到?
    • 或者,如果我将其更改为 NSArray,我要编写什么代码来追加和删除?
    • NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList") 和 weatherList = NSUserDefaults.standardUserDefaults().arrayForKey("weatherList").mutableCopy()。如果你想删除一个对象 self.weatherList.removeObjectAtIndex(index)
    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多