2017-04-06 4 views
-1

동일한 Collectionviewcell에 두 개의 tableview가 있습니다. 나는 응용 프로그램을 실행할 때, 그것을있는 tableview의 cellforrowatindexpath에 충돌 말한다 : "치명적인 오류가 : 선택 사양 값 풀기 동안 예기치 않게 전무 발견"그것은 tableview의 cellforrowatindexpath에서 충돌합니다 : "치명적인 오류 : 선택 값을 래핑하는 동안 예기치 않게 nil이 발견되었습니다"

내 코드의 일부를 검색 -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
UITableViewCell { 

let cell = collectionView.dequeueReusableCell(withReuseIdentifier:  
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as! 
MyCustomCollectionViewCell 

if(tableView == cell.graphTableView){ 

let cell:CustomGraphTableViewCell = 
tableView.dequeueReusableCell(withIdentifier: "CustomGraphTableViewCell") as!  
CustomGraphTableViewCell 

return cell 
    } 
    else 
    { 
     let cell:MyCustomTableViewCell = 
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as! 
MyCustomTableViewCell 
     cell.nameLabel.text = namesArray[indexPath.row] 
     return cell 

    } 
} 

수있는 사람하시기 바랍니다 해결책을 제안하십시오. 왜 그런 일이 일어나고 있으며 무엇을 해결할 수 있을까요?

+2

당신이'CollectionViewCell''를 큐에서 수를 TableView'cellForRowAt' 메소드. –

+0

당신은 "여기에 강제로 언 래핑을합니다!" 귀하의 모든 셀을 확인하십시오. 왜냐하면 지금은 그것의 nil –

+0

@ NiravD 어떤 생각 그럼 어떻게 내가 collectionview 세포를 선언하지 않으면 tableview에 액세스 할 수 있습니까? –

답변

2
의 tableview UICollectionViewCell에서

작동하지 않습니다, UICollectionViewCell는 셀 jQuery과 경우에만 사용해야합니다, 그래서 여기에 UICollectionReusableViewUITableViewCell에서 상속되는 다른 클래스가 UICollectionViewCell에서 다른 클래스와는 UIView, NSCoding, UIGestureRecognizerDelegate로부터 상속된다 당신의 tableview. 그렇지 않으면 충돌합니다. 당신의 tableview의 대리자 메서드는 방법 위의

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
UITableViewCell 

지금 실행 시작 몇 가지 값을 가질 때

당신의 tableview의 대리자 메서드는하지만 null 또는 0이 아닌 어떤 값을 반환

가 실행됩니다의 tableview의 다음 데이터 소스 방법, 즉 의미 발견했을 때

let cell = collectionView.dequeueReusableCell(withReuseIdentifier:  
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as! 
MyCustomCollectionViewCell 

위의 진술은 추락합니다.

let cell:MyCustomTableViewCell = 
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as! 
MyCustomTableViewCell 
     cell.nameLabel.text = namesArray[indexPath.row] 
     return cell 

만 cellforRow 데이터 소스 방식에 따라이 문을 넣어, 그것은 적절하게 작동합니다.

감사

+0

감사합니다. Abhishek! 네, cellforrowatindexpath에서 collectionviewcell 객체를 제거하고 tableview에 태그를 할당했습니다. 이제는 잘 작동합니다. :) –

+1

대단한 :) Happy Codding, 내 제안이 당신을 위해 일한다면 받아 들여서 제 답변을주세요. Thanks :) –

0

이 줄

cell.nameLabel.text = namesArray[indexPath.row] 

cell.nameLabel이 항상 먼저, 문자열 값을 받아이 같은 문자열 값으로 변환되어 있는지 확인하십시오

cell.nameLabel.text = namesArray[indexPath.row] as NSString 
관련 문제