我正在尝试复制Pinterest SDK的示例应用程序的方式:(https://github.com/pinterest/ios-pdk)在加载引脚的同时不断滚动,但我无法完全正确。任何人都能看到我错过的东西吗?此代码加载第一页(前25个引脚)但当你到达底部时不会重新加载下25个引脚。示例应用程序(和SDK)是用Obj-C编写的,所以我正在翻译,但我对Obj-C不是很熟悉。
private let reuseIdentifier = "PinCell"
class PinCollectionViewController: UICollectionViewController {
var pins = [PDKPin]()
var imageArray = [UIImage]()
var boardID = String()
var fetchingMore = false
var currentResponseObject = PDKResponseObject()
override func viewDidLoad() {
super.viewDidLoad()
PDKClient.sharedInstance().getBoardPins(boardID, fields: ["id", "image", "note", "link"], withSuccess: { (responseObject :PDKResponseObject!) -> Void in
self.currentResponseObject = responseObject
print(responseObject.pins().first!.descriptionText)
guard let pinsAsPDKPin = responseObject.pins() as? [PDKPin] else {print("problem downloading boards");return}
for pin in pinsAsPDKPin {
self.pins.append(pin)
print("\(pin.descriptionText) link = \(pin.url)")
}
let imageURLs = pinsAsPDKPin.map { $0.largestImage().url }
for url in imageURLs {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {(data, reponse, error) in
guard let data = NSData(contentsOfURL: url) else {return}
guard let image = UIImage(data: data) else {return}
self.imageArray.append(image)
dispatch_async(dispatch_get_main_queue(),{
self.collectionView?.reloadData()
})
})
task.resume()
}
dispatch_async(dispatch_get_main_queue(),{
self.collectionView?.reloadData()
})
}) { (err :NSError!) -> Void in
print("error NSError: \(err)")
}
}
override func viewWillAppear(animated: Bool) {
let imageURLs = pins.map {$0.largestImage().url}
for url in imageURLs {
guard let data = NSData(contentsOfURL: url) else {return}
guard let image = UIImage(data: data) else {return}
imageArray.append(image)
}
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArray.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PinCollectionViewCell
cell.pinText?.text = self.pins[indexPath.row].descriptionText
if imageArray.count > 0 {
if let readyImage = imageArray[indexPath.row] as? UIImage {
cell.pinImage?.image = readyImage
}
}
return cell
}
override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if self.fetchingMore == false && currentResponseObject.hasNext() && self.pins.count - indexPath.row < 5 {
self.fetchingMore = true
self.currentResponseObject.loadNextWithSuccess({ (nextResponseObject :PDKResponseObject!) -> Void in
self.fetchingMore = false
self.currentResponseObject = nextResponseObject
guard let unwrappedArrayOfPins = nextResponseObject.pins() as? [PDKPin] else {return}
for pin in unwrappedArrayOfPins {
self.pins.append(pin)
}
for url in imageURLs {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {(data, reponse, error) in
guard let data = NSData(contentsOfURL: url) else {return}
guard let image = UIImage(data: data) else {return}
self.imageArray.append(image)
dispatch_async(dispatch_get_main_queue(),{
self.collectionView?.reloadData()
})
})
task.resume()
}
dispatch_async(dispatch_get_main_queue(),{
self.collectionView?.reloadData()
})
}) { (err :NSError!) -> Void in
print("error NSError: \(err)")
}
}
}