我想在我的應用程序中使用解析並實現搜索功能,並希望使用重載數據對PFObjects進行排序。我的應用程序可以首次執行reloadData(),但它在第一次之後返回「意外地發現零,同時展開可選值」。我已經嘗試在mainThread中執行reloadData並刪除我的collectionView中的弱引用,但它仍然不起作用。我附上了我的代碼。先謝謝你!UICollectionView不重裝數據
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
loadCollectionViewData()
}
//This method is also conducted when the user taps search button
func loadCollectionViewData(){
let ud = NSUserDefaults.standardUserDefaults()
var query = PFQuery(className: "Posts")
if ud.objectForKey("searchKeyFromVCKey") != nil{
//This is keyword the user puts in
var searchKey = ud.objectForKey("searchKeyFromVCKey") as! String
println("searchKey \(searchKey)")
if searchKey != "" {
//If a user is searching something...
query.whereKey("searchTag", containsString: searchKey.lowercaseString)
}}
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
postObject.removeAll(keepCapacity: false)
if let object = objects as? [PFObject] {
postObject = object
}
self.collectionView.reloadData() //This works the first time when I conduct loadCollectionViewData() in ViewDidLoad
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
ud.removeObjectForKey("searchKeyFromVCKey")
}
哪個引用意外地爲零? –
@AaronBrager我不知道如何找到哪個引用是意外的零。通過此代碼,self.collectionView.reloadData()以綠色高亮顯示,併發生錯誤。 – Emily
將'postObject = object'改爲'postObject = object!' – DDPWNAGE