我在更新我的UITableView
時遇到問題。我有一個應用程序,基本UIViewController
與導航項「添加新項目」UITableViewContioller
。我可以添加字符串,然後轉儲數組以查看是否已添加。轉儲功能顯示陣列中添加的項目,但UITableView
不會,即使在致電reloadData
後。這是我的代碼。Swift - 添加到陣列後不能重新加載tableView
這裏是打保存後控制檯轉儲。
所以你可以看到的東西是有我保存後,但沒有顯示出來的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
}
}
你確定屏幕上顯示的'table'是你調用'refresh'的那個? –