2016-03-25 51 views
0

該控制器有一個collectionView,包括1個單元格,5個部分和一些行,從LeanCloud下載數據就像Parse。代碼總是失敗,致命錯誤:數組索引超出範圍。在我看來,我可能在處理數組的中遇到一些問題,關於如何訪問以及如何添加元素。任何人都可以幫助我解決這個錯誤?下面列出錯誤行:
var temp = self.restaurantLean [number]
致命錯誤:數組索引超出範圍。關於一個collectionView

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

如果您能告訴我們哪條線路導致錯誤,它總能幫助我們很多事情。 – Eendje

+0

對不起,這行似乎有問題:'var temp = self.restaurantLean [number]'@ Eendje –

+0

當我打印restaurantLean的計數時,它仍然是0.但是我在for循環中添加了元素,出了什麼問題與循環?@Eendje –

回答

0

您沒有添加元素restaurantLean陣列本身(只添加對象嵌套數組)。這是可能的解決方案。

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