【发布时间】: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