我在更新 UITableView
时遇到问题。我有一个应用程序,它有一个基本的 UIViewController
,带有一个 navigationItem 以“添加新”项目到 UITableViewContioller
。我可以添加字符串,然后转储数组以查看它是否已添加。转储函数显示数组中添加的项目,但 UITableView
没有显示,即使在调用 reloadData
之后也是如此。这是我的代码。
这是点击保存后的控制台转储。
因此您可以在我保存后看到一些内容,但 UITableview
上没有显示任何内容。
在 UIViewController
类中,我有一个 UIAlertAction
来接受文本和一个保存按钮。
let table = TableViewController()
let saveAction = UIAlertAction(title: "Save", style:UIAlertActionStyle.Default, handler: {
alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
self.table.cityArry.addObject(firstTextField.text!)
self.table.refresh()
})
来自 UITableViewController
类
class TableViewController: UITableViewController {
var cityArry:NSMutableArray = ["Local"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func refresh(){
dump(cityArry)
tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityArry.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let item = cityArry[indexPath.row]
let cell = UITableViewCell()
let label = UILabel(frame: CGRect(x:20, y:0, width:200, height:50))
label.text = item as? String
cell.addSubview(label)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Nothing here yet
}
}
最佳答案
我认为您不应该为显示的屏幕设置两个单独的 View Controller 。正如@J.Wang 所暗示的那样,简化可能最终会解决您在不同对象上调用方法的问题。您的屏幕应该使用嵌入到添加了按钮的导航 Controller 中的 TableViewController 得到很好的处理。
关于ios - Swift - 添加到数组后 tableView 不重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35715290/