所以我一直在寻找人们遇到这个问题的其他问题,但我似乎无法找到解决问题的方法。
我想使用一个按钮刷新并将新数据重新加载到我的tableview中,并使用相同的按钮来检索数据:
@IBOutlet var tv: UITableView!
@IBAction func refreshTapped(sender: AnyObject) {
var query = PFQuery(className:"textMessage")
query.whereKey("Receiver", equalTo:PFUser.currentUser().username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
NSLog("Successfully retrieved \(objects.count) scores.")
var image = UIImage()
for object in objects{
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let ene = NSEntityDescription.entityForName("MessageInformation", inManagedObjectContext: contxt)
var newMessage = Messages(entity: ene!, insertIntoManagedObjectContext: contxt)
let type = object["MessageType"] as Int
switch type{
case 0:
let message: String = object["Message"] as String
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).txt"
message.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
println(path)
newMessage.sender = object["Receiver"] as String
case 1:
let userImageFile = object["Image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
image = UIImage(data:imageData)
let imageToSave:NSData = UIImagePNGRepresentation(image)
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).png"
imageToSave.writeToFile(path, atomically: true)
println(path)
}
}
newMessage.sender = object["Receiver"] as String
case 2:
let message: String = object["Message"] as String
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).txt"
message.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
println(path)
newMessage.sender = object["Receiver"] as String
case 3:
let userImageFile = object["Image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
image = UIImage(data:imageData)
let imageToSave:NSData = UIImagePNGRepresentation(image)
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).png"
imageToSave.writeToFile(path, atomically: true)
println(path)
}
}
newMessage.sender = object["Receiver"] as String
default:
println("Type not found")
}
}
}else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
self.tv.reloadData()
}
这只是不起作用,我也很确定我的引用都很好。这也是我的viewDidLoad函数,如果有帮助:
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "TableViewCell", bundle: nil)
tv.registerNib(nib, forCellReuseIdentifier: "cell")
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let fetchReq = NSFetchRequest(entityName: "MessageInformation")
let en = NSEntityDescription.entityForName("MessageInformation", inManagedObjectContext: context)
let sortDescriptor = NSSortDescriptor(key: "dateReceived", ascending: false)
fetchReq.sortDescriptors = [sortDescriptor]
messageList = context.executeFetchRequest(fetchReq, error: nil) as [Messages]
tv.reloadData()
}
我现在已经在swift编程了一段时间,但仍然没有正确使用这个功能。
有关如何继续的任何建议将不胜感激。
编辑:我的cellForRowAtIndexPath-function:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "cellLongPressed:")
longPress.delegate = self
longPress.minimumPressDuration = 0.5
longPress.numberOfTouchesRequired = 1
let cell:TableViewCell = self.tv.dequeueReusableCellWithIdentifier("cell") as TableViewCell
cell.addGestureRecognizer(longPress)
let data = messageList[indexPath.row] as Messages
cell.labelOutl.text = data.sender
return cell
}
答案 0 :(得分:2)
你的self.tv.reloadData()
在闭包之外,所以当数据检索异步发生时,你会在检索数据之前立即调用reloadData
。
应该是 -
@IBOutlet var tv: UITableView!
@IBAction func refreshTapped(sender: AnyObject) {
var query = PFQuery(className:"textMessage")
query.whereKey("Receiver", equalTo:PFUser.currentUser().username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
NSLog("Successfully retrieved \(objects.count) scores.")
var image = UIImage()
for object in objects{
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let ene = NSEntityDescription.entityForName("MessageInformation", inManagedObjectContext: contxt)
var newMessage = Messages(entity: ene!, insertIntoManagedObjectContext: contxt)
let type = object["MessageType"] as Int
switch type{
case 0:
let message: String = object["Message"] as String
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).txt"
message.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
println(path)
newMessage.sender = object["Receiver"] as String
case 1:
let userImageFile = object["Image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
image = UIImage(data:imageData)
let imageToSave:NSData = UIImagePNGRepresentation(image)
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).png"
imageToSave.writeToFile(path, atomically: true)
println(path)
}
}
newMessage.sender = object["Receiver"] as String
case 2:
let message: String = object["Message"] as String
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).txt"
message.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
println(path)
newMessage.sender = object["Receiver"] as String
case 3:
let userImageFile = object["Image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
image = UIImage(data:imageData)
let imageToSave:NSData = UIImagePNGRepresentation(image)
let path = NSTemporaryDirectory() + "\(Int(self.createRandiAndSaveNumberToCell())).png"
imageToSave.writeToFile(path, atomically: true)
println(path)
}
}
newMessage.sender = object["Receiver"] as String
default:
println("Type not found")
}
}
dispatch_async(dispatch_get_main_queue(),{ ()->() in
self.tv.reloadData()
})
}else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
但是您还有另一个问题 - 您实际上没有使用newMessage
变量做任何事情来更新数据模型