Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 一个Viewcontroller中的多个CollectionView会导致索引超出范围错误_Ios_Swift_Uicollectionview - Fatal编程技术网

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

Ios 一个Viewcontroller中的多个CollectionView会导致索引超出范围错误,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我尝试在一个Viewcontroller中使用三个CollectionView。我按照以下方法解析数据: 在底部,我根据位置将数据添加到右侧列表中(此部分有效) 在我的NumbersofItemsInSection方法中,我做了以下同样有效的工作: func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if collectionVie

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

在我的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
    }
    }
在我的CellForRowaItem方法中,我尝试了以下方法

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

问题是,如果Second CollectionView中有更多项,我总是会出现以下错误:

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

在这一行代码中:

let eo = acceptedEvents[indexPath.row]

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
中返回的计数可能不同

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  
}