arrays - fatal error : Array index out of range.关于collectionView

标签 arrays swift parse-platform collectionview

Controller 有一个collectionView,包括1个cell、5个section和一些行,就像Parse一样从LeanCloud下载数据。代码总是失败并出现 fatal error :数组索引超出范围。在我看来,我在处理数组的数组时可能会遇到一些问题,关于如何访问以及如何添加元素。任何人都可以帮我解决这个错误吗?下面列出了错误行:
var temp = self.restaurantLean[数字]

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")
    }
}

最佳答案

您没有向 restaurantLean 数组本身添加元素(您仅向嵌套数组添加对象)。这是可能的解决方案。

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

关于arrays - fatal error : Array index out of range.关于collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213267/

相关文章:

javascript - 在 JavaScript 中一起使用过滤器和排序方法时排序不正确

ios - 检查互联网连接 queryForTable Parse

C 无法访问地址处的内存

c++ - 用 'this' 指针初始化 std::array

java - 从文本文件读取时如何忽略数字?

java - 解析.com : Does PinInBackground() to Local DB save to the Cloud eventually?

ios - 将原始图像数据转换为base64编码的字符串

swift - Swift 命令行程序中的 CFRunLoop

ios - 将来自 UITextField 的用户输入数据存储在 String 变量中

ios - Swift 2.0 ContactsUI 框架