2017-11-16 1 views
0

Ich habe eine CollectionView, auf die die Daten (String) von einer anderen VC nach dem Laden übergeben wird. Jetzt wenn ich diese Daten (String) an die UICollectionReusabelView übergeben möchte. Ich bin in den folgenden SchrittenDaten von Collectionview an UIcollectionResuableview übergeben

CollectionViewVC:

class PhotoDetailVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PDHeaderViewDelegate { 



// Following variables are the ones to which the data is passed to. 
var image : String! 
var user: User! 
var post: Posts! 


@IBOutlet weak var collectionView: UICollectionView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    print(post.imageURL) 

    collectionView.delegate = self 
    collectionView.dataSource = self 

    setupCells() 
    increaseView() 
} 


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "mainImage", for: indexPath) as! PDHeader 

    // I am setting the ReusableView properties through this section. Instead I just want to pass them to the header and then do stuff over there. 

    header.post = self.post 
    header.lol = post.postauthor 
    header.label.text = post.caption 
    header.label.sizeToFit() 
    header.delegate = self 
    let ref = URL(string: post.imageURL) 
    Nuke.loadImage(with: ref!, into: header.mainPic) 
    imageInHeaderForAuthor(author: post.postauthor, view: header.profilePic) 

    return header 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 

    let size = CGSize(width: collectionView.frame.width, height: collectionView.frame.height - 15) 
    return size 
} 


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

    let width = (collectionView.bounds.size.width/3) - 1 
    let size = CGSize(width: width, height: width) 
    return size 

} 

func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return 1.0 
} 

func collectionView(_ collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, 
        minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 1.0 
} 


func imageInHeaderForAuthor(author: String, view: UIImageView){ 
    FBDataservice.ds.REF_USERS.child(author).observeSingleEvent(of: .value, with: { (snapshot) in 
     if let allData = snapshot.value as? Dictionary<String, Any> { 
      if let cred = allData["credentials"] as? Dictionary<String, Any> { 
       let postuser = User(userid: author, userData: cred) 
       if postuser.userPic != nil { 
        let req = URL(string: postuser.userPic) 
        Nuke.loadImage(with: req!, into: view) 
       } 
      } 
     } 
    }) 
} 

} 

ReusableView:

class PDHeader: UICollectionReusableView, FloatyDelegate { 

@IBOutlet weak var label: UILabel! 
@IBOutlet weak var mainPic: UIImageView! 
@IBOutlet weak var profilePic: UIImageView! 

var image : String! 
var post: Posts! 
var lol: String! 
var floaty = Floaty() 
var isOwner : Bool! 
var delegate: PDHeaderViewDelegate! 



override func awakeFromNib() { 
    super.awakeFromNib() 

    //This returns nil every time. 
    print(post.postauthor) 

    layoutFAB() 
    profilePic.makeCircle() 
    label.addDropShadow(opacity: 3, radius: 8) 

} 




func setDelegate(delegate: PDHeaderViewDelegate) { 
    self.delegate = delegate 
} 

func layoutFAB() { 
    floaty.openAnimationType = .pop 
    floaty.buttonImage = UIImage(named: "options") 
    floaty.addItem("Report", icon: UIImage(named: "trash")) { item in 
     self.delegate.fabReportClicked() 
    } 
    if isOwner{ 
     floaty.addItem("Edit Profile", icon: UIImage(named: "edit")) { item in 
      self.delegate.fabEditClicked() 
     } 
     floaty.addItem("Delete", icon: UIImage(named: "trash")) { item in 
      self.delegate.fabDeleteButton() 
     } 
    } 

    floaty.fabDelegate = self 
    floaty.buttonColor = UIColor.white 
    floaty.hasShadow = true 
    floaty.size = 45 
    addSubview(floaty) 
} 

} 

ich den Code mit den Themen kommentiert haben. TL; DR - Die Sammlungen übergeben keine Daten an die Kopfzeile und müssen daher von viewForSupplementaryElementOfKind gesetzt werden.

was ist das eigentliche Problem hier? Wird der Header geladen, bevor die Daten an ihn übergeben werden? Wenn nicht, wie kann ich das beheben?

+0

1) Post-Daten Null in awakeFromNib alway wird als Variable noch nicht festgelegt ist. 2) Es gibt Mehrdeutigkeiten ** header.post = self.post header.post = post.postautor **. Sie setzen zweimal auf eine Variable .. es kann dazu führen, dass post.postauthour ist –

+0

Bearbeitete meine Frage. War ein Fehler beim Einfügen des Codes –

+0

Sie haben nicht erwähnt, was das eigentliche Problem ist? wird das Bild nicht geladen? –

Antwort

1

Wenn ich verstanden haben, können Sie mit dieser Option versuchen:

class PDHeader: UICollectionReusableView, FloatyDelegate { 

    @IBOutlet weak var label: UILabel! 
    @IBOutlet weak var mainPic: UIImageView! 
    @IBOutlet weak var profilePic: UIImageView! 

    var image : String! 
    var lol: String! 
    var floaty = Floaty() 
    var isOwner : Bool! 
    var delegate: PDHeaderViewDelegate! 

    var post: Posts!{ 
     didSet { 
      lol = post.postauthor 
      label.text = post.caption 
      // etc ... 
     } 

Dann:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "mainImage", for: indexPath) as! PDHeader 

    header.post = self.post 
    header.delegate = self 

    return header 
} 
+0

Hey, schätze die Hilfe. Diese Lösung funktioniert ganz gut, aber sobald ich versuche, auf den "lol" außerhalb von 'didset' zuzugreifen, gibt es null –

+1

Hallo, wenn Sie Ihren lol Wert außerhalb Post didSet, zum Beispiel in einer Funktion, Sie brauchen kann das gleiche tun und es in lol didset aufrufen. Sie erhalten NULL, weil Sie den Wert nicht haben, der in dem Moment festgelegt ist, in dem Sie ihn aufrufen. Wenn Sie jedoch Ihre Header-Variablen in Ihrem ViewController wiederverwenden möchten, können Sie nach dem Setzen des Header-Posts alle Ihre Werte abrufen. –

+0

Fantastisch! Das macht meine Arbeit! –

Verwandte Themen