【发布时间】:2017-03-30 16:35:46
【问题描述】:
我制作了一个使用表格视图来保存数据的应用。表格视图显示了通过 UserDefaults 保存的数组元素。由于 TableViewControllers 看起来有点难看,所以我做了一点不同:我把一个 TableView 放到了一个普通的 ViewController 中。 我的问题是:尽管我尽我所能让 TableView 显示数据,但它没有,而且我不知道为什么。谁能帮助我? 这是 ViewController 的代码:
class ErfolgreicheChallenges: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var myTableView: UITableView!
var data = [String]()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
data.append("test")
data.append("lol")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
myTableView.dataSource = self
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = data[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
感谢大家观看!祝你有美好的一天
【问题讨论】:
-
放弃 UITableViewController 可能是一个不错的决定,但使用常规 VC 需要一些额外的步骤。其中最重要的是将tableView的数据源设置为包含发布代码的类。你这样做了吗?
-
在表格视图中你在哪里调用
reloadData()? -
好吧,当我在viewDidLoad()中将数据源设置为self时,总是会出现'Thread 1'-error之类的,不知道是什么问题...或者do你的意思是别的吗?
-
试过了,但是当我尝试 reloadData() 时我的应用程序失败了...你认为我需要在哪里调用它?
-
另外——你不是在表格视图中保存数据,你只是在显示它
标签: ios objective-c uitableview swift3 userdefaults