ios - Swift - 添加到数组后 tableView 不重新加载

标签 ios arrays xcode swift uitableview

我在更新 UITableView 时遇到问题。我有一个应用程序,它有一个基本的 UIViewController,带有一个 navigationItem 以“添加新”项目到 UITableViewContioller。我可以添加字符串,然后转储数组以查看它是否已添加。转储函数显示数组中添加的项目,但 UITableView 没有显示,即使在调用 reloadData 之后也是如此。这是我的代码。

enter image description here

这是点击保存后的控制台转储。

enter image description here

因此您可以在我保存后看到一些内容,但 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/

相关文章:

c++ - Qt 在 Mac OS X 上的安装问题

swift - "Use of undeclared identifier ' __BYTE_ORDER ' "更新 Pod 后出错

ios - 在 iOS 中调试生产应用程序?

c# - Xamarin iOS - 模拟器 View 不刷新

ios - 在 UIAlertController 中更改标题的背景颜色(仅)

ios - 使用 API 推送通知

php - 递归添加元素到数组

c++ - 使用模板编译时计算使用 const 值初始化数组

java - 无法将 setArray JDBC(整数)与 H2 数据库一起使用

swift - 如何将 sdk 15.6 添加到 Xcode 13.4.1