我在ViewController中有一个TextField + Button + TableView。
项目运行,导入OK。但我必须重新启动模拟器,TableView现在显示值。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var myArray = [String]()
@IBOutlet weak var txtName: UITextField!
@IBAction func button(sender: AnyObject) {
var delegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = delegate.managedObjectContext!
var newName = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context) as! NSManagedObject
newName.setValue(txtName.text, forKey: "name")
context.save(nil)
txtName.text = ""
}
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
var delegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = delegate.managedObjectContext!
var export = NSFetchRequest(entityName: "Entity")
export.returnsObjectsAsFaults = false
var exportValue = context.executeFetchRequest(export, error: nil)
for ketqua: AnyObject in exportValue! {
myArray.insert((ketqua.valueForKey("name") as! String), atIndex: 0)
}
self.myTableView.delegate = self
self.myTableView.dataSource = self
self.myTableView.registerClass(myCell.self, forCellReuseIdentifier: "Cell")
viewDidAppear(true)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
myTableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! myCell
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
}
答案 0 :(得分:0)
在您的数据结构完成获取信息/数据之后,您应该在{strong>将数据插入数组之后调用reloadData()
。
override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
getMydata() //<-- call the function here
}
func getMydata()
{
var delegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = delegate.managedObjectContext!
var export = NSFetchRequest(entityName: "Entity")
export.returnsObjectsAsFaults = false
var exportValue = context.executeFetchRequest(export, error: nil)
for ketqua: AnyObject in exportValue!
{
myArray.insert((ketqua.valueForKey("name") as! String), atIndex: 0)
}
myTableView.reloadData()
// and if you want use dispatch to get to main_queue again
}
答案 1 :(得分:-1)
我改变了你的代码,这对你有用。正如vadian所提到的,你不应该自己调用viewDidAppear()。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var myArray = [String]()
private var delegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
@IBOutlet private weak var txtName: UITextField!
@IBOutlet private weak var myTableView: UITableView!
@IBAction func button(sender: AnyObject) {
var context: NSManagedObjectContext = delegate.managedObjectContext!
var newName = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context) as! NSManagedObject
newName.setValue(txtName.text, forKey: "name")
context.save(nil)
txtName.text = ""
}
override func viewDidLoad() {
super.viewDidLoad()
var context: NSManagedObjectContext = delegate.managedObjectContext!
var export = NSFetchRequest(entityName: "Entity")
export.returnsObjectsAsFaults = false
var exportValue = context.executeFetchRequest(export, error: nil)
for ketqua: AnyObject in exportValue! {
myArray.insert((ketqua.valueForKey("name") as! String), atIndex: 0)
}
self.myTableView.registerClass(myCell.self, forCellReuseIdentifier: "Cell")
myTableView.delegate = self
myTableView.dataSource = self
myTableView.reloadData()
}
// MARK: - UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! myCell
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
}