【问题标题】:Refresh Table Contents When View Reappears视图重新出现时刷新表格内容
【发布时间】:2014-12-15 19:24:37
【问题描述】:

我正在写一个笔记应用程序,仅供参考。我设置了数组,并使用以下代码提供了一个表格:

import UIKit
import Foundation

var tableData = ["Pancake Recipe", "Costume Party", "Camping Supplies"]
var tableSubtitle = ["Some Milk and some Flour", "Let's dress up like Jen", "Tenting with Lucy"]

class ViewController: UIViewController {



func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return tableData.count
}


func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
    cell.textLabel!.text = tableData.reverse()[indexPath.row]
    cell.detailTextLabel!.text = tableSubtitle.reverse()[indexPath.row]

    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()

    var listTitle = "Notes"
    self.title = listTitle

    UIApplication.sharedApplication().statusBarStyle = .LightContent
}

override func viewDidAppear(animated: Bool) {
    println(tableSubtitle)

}

}

用户为不同页面上的单元格创建新标题和副标题,并将它们添加到数组(tableData 和 tableSubtitle 数组)中。我知道数据的添加工作正常,因为当我观看控制台时,它完美地打印了两个更新的数组。

然后当我返回主视图控制器时,我看到了一个额外的单元格(如我所愿),但不是我想要的新内容,而是“煎饼食谱”单元格的副本。

再次加载视图时是否需要刷新单元格的内容?如果是这样,我该怎么做?

谢谢:)

作为参考,这是两次向两个数组添加数据后的表格视图图片,然后我返回到表格视图,尽管两个数组现在都包含两个额外且不同的条目(已检查使用 println(tableData) 和 println(tableSubtitle)

【问题讨论】:

  • 你能添加你用来添加新单元格的代码吗?那么或许可以为你提供更有用的答案

标签: ios arrays swift view


【解决方案1】:

提供的代码没有提供太多信息来查找问题,可能问题出在数据添加代码上。

要刷新表格视图,请使用:

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    self.yourTableView.reloadData()
}

【讨论】:

  • 我怎么知道用什么替换'yourTableView'?对不起!
  • @Mydogmaxieboy:这是您的 tableview 的出口名称,如果您使用的是 UITableViewController,那么 tableView 将是名称。
  • 只需像你说的那样添加一个 IBOutlet 就可以了!非常感谢你!你真是天才!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2011-03-16
相关资源
最近更新 更多