2016-03-25 20 views
0

Denetleyici, 0 hücre, 5 bölüm ve bir satır da dahil olmak üzere collectionView koleksiyonuna sahiptir, LeanCloud'dan veri indirmek gibi sadece Parse. Kod her zaman ölümcül hatayla başarısız olur: Dizi dizini aralık dışı. Benim düşünceme göre dizisinde dizisinin dizilimi ile ilgili bir sorunum olabilir. Herhangi biri bu hatayı çözmeme yardım edebilir mi? Hata satırı aşağıda listelenmiştir:
var temp = self.restaurantLean [sayı].Önemli hata: Dizi dizini aralık dışı. Hakkında bir koleksiyonView

import UIKit 
import AVOSCloud 

class DiscoverViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, RestaurantLeanCollectionCellDelegate, UIGestureRecognizerDelegate { 


@IBOutlet var imageView: UIImageView! 

@IBOutlet var collectionView: UICollectionView! 

private var restaurantLean = [[RestaurantLean]]() 

override func viewDidLoad() { 

    super.viewDidLoad() 

    collectionView.backgroundColor = UIColor.clearColor() 

    loadTripsFromLeanCloud()  

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

//MARK: Data Source 
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return restaurantLean.count 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return restaurantLean[section].count 
} 


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! RestaurantLeanCollectionCell 
    cell.delegate = self 
    cell.nameLabel.text = restaurantLean[indexPath.section][indexPath.row].name 
    cell.typeLabel.text = restaurantLean[indexPath.section][indexPath.row].type 
    cell.locationLabel.text = restaurantLean[indexPath.section][indexPath.row].location 
    cell.isLike = restaurantLean[indexPath.section][indexPath.row].isLike 
    cell.imageView.image = UIImage() 
    cell.layer.cornerRadius = 4.0 
    if let image = restaurantLean[indexPath.section][indexPath.row].image { 
      image.getDataInBackgroundWithBlock({ (imageData, error) -> Void in 
       print(image) 
       if let data = imageData { 
        print("loading") 
        cell.imageView.image = UIImage(data: data) 
        print("success") 
       } 
      }) 
     } 
    return cell 
} 

//Download the data from Baas LeanCloud 

func loadTripsFromLeanCloud() { 

    restaurantLean.removeAll(keepCapacity: true) 

    for number in 0...4 { 
     let name = "Restaurant_" + String(number) 
     print(name) 
     print(number) 
     let query = AVQuery(className: name) 
     query.cachePolicy = AVCachePolicy.NetworkElseCache 
     print("1") 
     query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
      print("2") 
      if let error = error { 
       print("3") 
       print("Error: \(error) \(error.userInfo)") 
      } 
      print("4") 
      if let objects = objects { 
       print("5") 
       for (index, object) in objects.enumerate() { 
        let restaurant = RestaurantLean(avObject: object as! AVObject) 
        self.restaurantLean[number].append(restaurant) 
        let indexPath = NSIndexPath(forRow: index, inSection: number) 
        self.collectionView.insertItemsAtIndexPaths([indexPath]) 

       } 
      } 
     }) 
     print("6") 
    } 
} 
+1

Bize hangi satırın hataya neden olduğunu söylerseniz, her zaman çok yardımcı olur. – Eendje

+0

Üzgünüz, bu satırda sorun var gibi görünüyor: 'var temp = self.restaurantLean [numara]' @ Eendje –

+0

Ben restaurantLean sayısını yazdığımda, hala 0'dır. Ancak, for döngüsünde ona eleman ekledim, neyin var? döngü ile @ Eendje –

cevap

0

Sen kendisi (yalnızca iç içe diziler nesneleri ekleyin) restaurantLean diziye öğe ekleyebilen değildir. İşte olası çözüm.

func loadTripsFromLeanCloud() { 
    restaurantLean.removeAll(keepCapacity: true) 
    for number in 0...4 { 
     restaurantLean.append([]) // This line 
     // ... 
    } 
}