Ios 重新显示视图时刷新表内容
我正在写一个笔记应用程序,仅供参考。我已经设置了数组,并且有一个表,该表使用以下代码从数组中获取信息:Ios 重新显示视图时刷新表内容,ios,arrays,swift,view,Ios,Arrays,Swift,View,我正在写一个笔记应用程序,仅供参考。我已经设置了数组,并且有一个表,该表使用以下代码从数组中获取信息: 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
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)检查)
提供的代码没有提供太多信息来查找问题,问题可能是数据添加代码 要刷新表视图,请使用:
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.yourTableView.reloadData()
}
你能添加你用来添加新单元格的代码吗?然后也许能为你提供一个更有用的答案我怎么知道用什么来替换“yourTableView”?对不起!@Mydogmaxieboy:这是你的tableview的插座名称,如果你使用的是
UITableViewController
,那么tableview
就是这个名称。只需为它添加一个IBOutlet即可就像你说的那样,非常好!非常感谢!你真是个天才!