【问题标题】:Fatal error: Array index out of range. About a collectionView致命错误:数组索引超出范围。关于 collectionView
【发布时间】:2016-07-12 19:34:30
【问题描述】:

控制器有一个collectionView,包括1个cell,5个section和一些row,像Parse一样从LeanCloud下载数据。代码总是失败并出现致命错误:数组索引超出范围。在我看来,我在处理数组数组方面可能存在一些问题,关于如何访问和如何添加元素。任何人都可以帮我解决这个错误吗?下面列出了错误行:
var temp = self.restaurantLean[number]

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

【问题讨论】:

  • 如果你能告诉我们是哪一行导致了错误,它总是对我们有很大帮助。
  • 抱歉,这条线好像有问题:var temp = self.restaurantLean[number]@Eendje
  • 当我打印restaurantLean的计数时,它仍然是0。但是我在for循环中添加了元素,循环有什么问题?@ Eendje
  • restaurantLean.removeAll(keepCapacity: true) 仅使用 restaurantLean.removeAll()。
  • 即使我按照您的建议更改了它,仍然会出现相同的错误。 @达里

标签: arrays swift parse-platform collectionview


【解决方案1】:

您不会将元素添加到restaurantLean 数组本身(您只是将对象添加到嵌套数组)。这是可能的解决方案。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多