7

메시지 확장 스티커 팩에 스토리 보드를 사용하지 않기로 선택했습니다. iMessage 앱에는 스토리 보드 파일과 MessagesViewController.swift가 함께 제공됩니다. CollectionViewController와 StickerCell이라는 이름의 2 개의 파일을 만들었습니다. 이 아이디어는 CollectionViewCell을 서브 클래스 화하여 MSStickerView로 캐스팅하고 해당 뷰를 내 CollectionView로 "셀"로 디큐하는 것입니다. 나는 여기에있다 나는이 문제를 추측하고있어스티커 팩을 프로그래밍 방식으로 생성, UICollectionViewCell을 MSStickerView로 캐스팅 할 때 발생합니다.

class StickerCell: UICollectionViewCell { 
    var stickerView: MSStickerView! 
} 

을 :

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let item = data[indexPath.row] 
    return deQStickerCell(for: item, at: indexPath) 
} 

private func deQStickerCell(for sticker: MSSticker, at indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! StickerCell 
    cell.stickerView.sticker = sticker 
    return cell 
} 

내 StickerCell 클래스의 코드 : 여기

는 MSSstickerView로 "StickerCell"을 설정하는 코드입니다 StoryClass의 스토리 보드와 정확한 코드로이 작업을 성공적으로 마쳤습니다. 단, var는 IBOutlet이었습니다. 그래서 뭔가 명확하게 CollectionView에 연결되어 있지 않거나 단계를 놓쳤습니다. Interface Builder에서 CollectionViewController를 만들고, CollectionView에주고, CollectionViewCell을주고, 맨 위에 UIView를 쳐서 MSStickerView로 변경합니다.

프로그래밍 방식으로 Interface Builder 워크 플로를 어떻게 다시 작성합니까?

답변

1

문제가 무엇인지 정확히 알기는 어렵지만, stickerView의 하위보기로 StickerCell을 돌려받지 못한다고 말할 수 있습니다. IB와 다른 점은 스티커보기를 초기화하지 않는다는 것입니다. 뷰를 만들고 셀에 추가하는 init을 추가해야합니다. 다음과 같은 것 (의사 코드) :

override init(frame: CGRect) { 
    super.init(frame: frame) 

    self.stickerView = StickerView() 
    self.contentView.addSubview(self.stickerView) 
    // Set up your constraints & layout 
} 
+0

Ohhhhh. 나는 IB가 나를 위해하는 일을 당연한 것으로 생각한다. 그래서 내가 프로그래밍 방식으로 일을하도록 강요합니다. 나는이 같은 것을 줄 것이다. –

관련 문제