2016-11-24 6 views
0

내 코드 업데이트 후이 있습니다 :오류 : 섹션 0으로 항목에 0을 삽입을 시도하지만, 섹션 0 만 0 항목

DataService.dataService.fetchDataFromServer { (channel) in 
     self.channels.append(channel) 
     let indexPath = IndexPath(item: self.channels.count - 1, section: 0) 
     self.collectionView?.insertItems(at: [indexPath]) 
    } 

는 서버 기능에서 데이터를 가져 오기 :

func fetchDataFromServer(callBack: @escaping (Channel) ->()) { 
     DataService.dataService.CHANNEL_REF.observe(.childAdded, with: { (snapshot) in 
      let channel = Channel(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>) 
      callBack(channel) 
     }) 
    } 

번호는

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of items 
    return 0 
} 

전체 오류 :

항목 섹션의
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update' 

컬렉션보기로 작업 중이며이 오류가 표시되는 이유를 찾을 수 없습니다.

도움이 될 것입니다.

+0

이 – Paulw11

+0

단지를 추가하여'numberOfItemsInSection' 방법을 표시 :

코드는 심지어 코멘트 // #warning Incomplete implementation, return the number of items

당신은 아마 뭔가를 원하는에게 있습니다. 보세요! –

+0

0을 돌려줍니다. 아마도'self.channels.count'를 돌려 주길 원할 것입니다. – Paulw11

답변

2

numberOfItemsInSection은 0을 반환하므로 컬렉션보기에 아이템을 추가한다고 말하면이 메소드가 콜렉션에 아직 0 개의 아이템이 있다고 표시하면 화가납니다.

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return self.channels.count 
} 
+0

아니요, 다음에 기반하여 나열된 문제에 대한 해결책입니다. 귀하가 제공 한 정보. 여전히 동일한 예외가 발생합니까? – Paulw11

+0

예 나는 여전히 같은 오류가 발생하므로 작동하지 않습니다. 다른 해결책이 있습니다. 내 의견을 잘못 말하면서 유감입니다. –

+1

내가 말했듯이, 당신이 보여준 것에 근거하여 다른 것을 제안 할 수는 없지만, 'numberOfItemsInSection'에있는 코드를 기반으로 다른 것들을 설정하지 않았을 가능성이 있습니다. – Paulw11