2017-01-17 3 views
0

Xcode 8과 Swift 3을 사용하고 있습니다. UICollectionView를 디자인하고 있었기 때문에 높이와 너비를 동적으로 설정하여 일부 답변을 원했습니다. 길에 나는 (수정 여부 - 나는 아무 생각이 없음)이 솔루션을 가지고 :UICollectionViewDelegate의 Swift 3 경고

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let Labell : UILabel = UILabel() 
    Labell.text = self.items[indexPath.item] 
    let labelTextWidth = Labell.intrinsicContentSize.width 
    return CGSize(width: labelTextWidth + 20, height: 35) 

} 

을하지만이 답변이 신속 2.0에 있었고,이 답을 준 사람은 하나를 해결했다고 주장. 나는 Xcode에 붙여 넣었고 Xcode가 나에게 제안한 바를 바꿨다. 내가 시도

재미 키워드 앞에 @nonobjc 추가)

instance method 'collectionView:layout:sizeForItemAtIndexPath:)' nearly matches optional requirement 'collection(_:willDisplaySupplementaryView:forElement:at:)' of protocol 'UICollectionViewDelegate' 

엑스 코드 나 2 개 솔루션 1 제안) 개인 앞서 FUNC 키워드 2의 추가

지금 여기에서 나는 다음과 같은 경고를 얻고있다 두 솔루션 모두 경고를 표시하지만 위의 경고는 표시되지 않습니다. 나는 브레이크 포인트를 두어 보았고 여러 가지 방법을 시도했다. 누군가가 나를이 구덩이에서 끌어낼 수 있다면.

+0

내가 가지고 그 방법 FUNC의 collectionView (_ collectionView : UICollectionView, 레이아웃 collectionViewLayout : UICollectionViewLayout, sizeForItemAt indexPath : IndexPath) -> CGSize { } –

+0

은 https ://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd#.hjsujieml 이것을 참조하십시오 –

답변

1

UICollectionViewDelegate에 대한 sizeForItemAtIndexPath 대리자 메서드가 없습니다. 아마 당신은 무엇을 찾고 있는지

UICollectionViewDelegateFlowLayout 프로토콜에서 찾을 수 있습니다

optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 

입니다.

것은이와 코드를 교체하십시오 :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let Labell : UILabel = UILabel() 
    Labell.text = self.items[indexPath.item] 
    let labelTextWidth = Labell.intrinsicContentSize.width 
    return CGSize(width: labelTextWidth + 20, height: 35) 

} 
+0

감사합니다 @ masttsson –