首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命错误:数组索引超出范围。关于一个collectionView

致命错误:数组索引超出范围。关于一个collectionView
EN

Stack Overflow用户
提问于 2016-03-25 02:47:53
回答 1查看 690关注 0票数 0

控制器有一个collectionView,,包括1个单元、5个部分和一些行,从LeanCloud下载数据就像Parse一样。代码总是失败的致命错误:数组索引超出范围。在我看来,在处理数组数组时,我可能会遇到一些问题,比如如何访问和如何添加元素。有人能帮我解决这个问题吗?错误行列如下:

var temp = self.restaurantLeannumber.

代码语言:javascript
复制
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")
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 08:42:33

您没有向restaurantLean数组本身添加元素(只向嵌套数组添加对象)。这里有可能的解决办法。

代码语言:javascript
复制
func loadTripsFromLeanCloud() {
    restaurantLean.removeAll(keepCapacity: true)
    for number in 0...4 {
        restaurantLean.append([])  // This line
        // ...
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36213267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档