之后重新加载UITableview

时间:2016-05-18 13:49:45

标签: ios swift tableview

我的View上有简单的表格。另一个视图具有一些值的数组。当我回来时,我想用新值重新加载表,但是不行。

我的课程:

 let textCellIdentifier = "cell"
var menu:[[String]] = [[]]
var buscaEmp:BuscadorEmpresa = BuscadorEmpresa()


@IBOutlet weak var tablaVista: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()
    recuperaEmpresas()
    tablaVista.delegate = self
    tablaVista.dataSource = self

}

func recuperaEmpresas(){

      menu = buscaEmp.getEmpresas()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return menu.count
}




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

    let row = indexPath.row
    cell.nombreEmp.text = menu[row][0]
    return cell

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    recuperaEmpresas()
    tablaVista.reloadData()
}

我在重新打开视图之前使用viewWillAppear重新加载表。

1 个答案:

答案 0 :(得分:2)

在viewWillAppear中

,您必须致电recuperaEmpresas(),然后致电tableView.reloadData()

这样,它将使用来自recuperaEmpresas()的新数据重新填充菜单数据源,并使用新的数据源值重新加载表视图。

编辑:
并确保recuperaEmpresas()使用新数据刷新数据,如果它始终相同,则您不会看到任何更改......