【问题标题】:I am using Swift 3. I want to write my name in the text field & when I press the button, I want my name to appear on the TableView list我正在使用 Swift 3。我想在文本字段中写下我的名字,当我按下按钮时,我希望我的名字出现在 TableView 列表中
【发布时间】:2017-08-21 22:18:30
【问题描述】:

我仍然是 Swift 的初学者……我正在使用 XCode 8、Swift 3.0(单视图应用程序)……我有一个UITableView、一个UITextField 和一个UIButton。我想在UITextField 中写下我的名字,当我点击UIButton 时,我希望我的名字出现在UITableView 的列表中。

这是我写的代码... 每次我写下我的名字并按下UIButton,我的名字都不会出现在UITableView 上。

import UIKit

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource  {

    var list = ["Dina", "Sam", "Stremmel"]

    @IBOutlet weak var TableView: UITableView!


    @IBOutlet weak var input: UITextField!


    @IBAction func Addname(_ sender: Any)

    {

        if (input.text != "")

        {
            list.append(input.text!)



            input.text = ""


        }

    }

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

    {
        return (list.count)

    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell


    {

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

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

        return (cell)


    }


     public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

    {
        if editingStyle == UITableViewCellEditingStyle.delete
        {
            list.remove(at: indexPath.row)
            tableView.reloadData()
        }


    }

    override func viewDidAppear(_ animated: Bool)
    {

        reloadInputViews()
    }

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

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


}

【问题讨论】:

    标签: ios objective-c xcode swift3


    【解决方案1】:

    改变

    @IBAction func Addname(_ sender: Any) {
        if (input.text != "") {
            list.append(input.text!)
            TableView.reloadData() // this line
            input.text = ""
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 2019-05-22
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 2022-11-12
      相关资源
      最近更新 更多