ios - 将文本字段输入表格时出现问题

标签 ios swift uitableview uitextfield

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

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")

}

}

最佳答案

来自NSUserDefaults class reference :

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

那不是 swift 数组。使用 NSMutableArray 代替:

var weatherList:NSMutableArray = NSMutableArray()

然后

self.weatherList.addObject(textField.text)

关于ios - 将文本字段输入表格时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748445/

相关文章:

uitableview - 在 iOS 11 中滚动到表格 View 的顶部

ios - 优化 openGL ES 2.0 2D 纹理输出和帧率

ios - 当位于 subview 时,我无法将按钮的 touchUpInside 调用到 ViewController

UITableView 从顶部到 topLayoutGuide 以编程方式插入内容

ios - 使用解析和 url 填充 UItableview

ios - 为什么在 "numberOfSectionInTableView:"中使用 NSInteger 而不是 NSUInteger?

objective-c - 将十进制转换为二进制(字符的位图)

ios - 位于最左上角的单元格中的标签?

ios - UINavigationController 推送方法在动画完成之前加载下一个 VC 元素

swift - swift 中的基本 tcp/ip 服务器