一个Viewcontroller中的多个collectionview导致索引超出范围错误

时间:2018-12-21 21:46:54

标签: ios swift uicollectionview

我尝试在一个Viewcontroller中使用三个collectionview。我解析数据,如以下方法所示: 在底部,我根据位置将数据添加到右侧列表中(此部分有效)

 func getEventData(eventIDs: [String], plz: String, positiona: Int){


        for eventId in eventIDs {
        let ref = Database.database().reference().child("Events").child(plz).child(eventId)
         ref.observe(.value, with: { snapshot in
            let item = snapshot.value as? [String: AnyObject]
            let eventName = item?["name"] as! String
            let date = item?["date"] as! String
            let lat = item?["lat"] as! String
            let lng = item?["lng"] as! String
            let infos = item?["additionalInfos"] as! String
            let position = item?["position"] as! String
            let ts = item?["ts"] as! Int
            let createdBy = item?["createdBy"] as! String

         let timestamp = NSDate().timeIntervalSince1970
            if (ts > Int(timestamp)) {
                let eo = EventObject(eventID: eventId, eventName: 
   eventName, info: infos, createdBy: createdBy, date: date, lat: lat, lng: lng, position: position, ts: ts)
                if positiona == 0{
                    self.acceptedEvents.append(eo)
                    self.acceptedEventscv.reloadData()

                }else if positiona == 1{
                    self.myEvents.append(eo)
                    self.myEventscv.reloadData()

                }else if positiona == 2{
                   self.storedEvents.append(eo)
                self.storedEventscv.reloadData()
                }
            }

         })
        }
    }

在我的NumbersofItemsInSection方法中,我执行了以下操作:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.acceptedEventscv{
    return acceptedEvents.count
    }else if collectionView == self.storedEventscv{
        return storedEvents.count
    }else if collectionView == self.myEventscv{
        return myEvents.count
    }else{
        return 0
    }
    }

在我的CellForRowAtItem方法中,我尝试了以下操作

func collectionView(_ collectionView: UICollectionView, 
 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 if collectionView == self.acceptedEventscv{
 let cell = 
  collectionView.dequeueReusableCell(withReuseIdentifier: 
"acceptedEventsCell", for: indexPath) as! 
 acceptedEventsCollectionViewCell
 let eo = acceptedEvents[indexPath.row]
  cell.eventName.text = eo.eventName

  let items = eo.date!.components(separatedBy: " ")//Here replase 
  space with your value and result is Array.
 //Direct line of code
 //let items = "This is my String".components(separatedBy: " ")
 let date = items[0]
let time = items[1]
cell.date.text = date
cell.time.text = time

getUserNameAge(label: cell.usernameAge)
return cell
}else {
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "savedEventsCell", for: indexPath) as! savedEventsCollectionViewCell
  let eo = acceptedEvents[indexPath.row]
  cell.eventName.text = eo.eventName

  let items = eo.date!.components(separatedBy: " ")//Here replase space with your value and result is Array.
  //Direct line of code
  //let items = "This is my String".components(separatedBy: " ")
  let date = items[0]
  let time = items[1]
  cell.date.text = date
  cell.time.text = time


  getUserNameAge(label: cell.usernameAge)

  return cell
}

}

问题是,如果我在第二个CollectionView中有更多项目,我总是会收到此错误:

  

线程1:致命错误:索引超出范围

此行代码:

let eo = acceptedEvents[indexPath.row]

1 个答案:

答案 0 :(得分:1)

cellForItemAt也必须像

if collectionView == self.acceptedEventscv{
  let item = acceptedEvents[indexPath.row]
   ----
  return cell
}else if collectionView == self.storedEventscv{
    let item = storedEvents[indexPath.row]   
    ----
    return cell
}else {
     let item = myEvents[indexPath.row] 
     ----
     return cell  
}

在您的情况下,现在发生的情况是,在两种情况下,您访问相同的数组acceptedEvents,其中numberOfItemsInSection中返回的计数可能不同