将数据从CollectionviewCell传递到ViewController

时间:2018-07-20 15:23:46

标签: swift uicollectionviewcell

当我编写函数touchphoto时,post.postID为nil。我想将postID传递给另一个视图控制器。我添加了profilecell和profileviewcontroller的代码

protocol profilecellDelegate {
func goToPhotoDetail(postid: String)}

class profilecell: UICollectionViewCell {
var delegate: profilecellDelegate?

@IBOutlet weak var image: UIImageView!

var post: Post? {
    didSet {
        updateView()
    }
}

func updateView() {
    if let photoUrlString = post?.photoUrl {
        let photoUrl = URL(string: photoUrlString)
        image.sd_setImage(with: photoUrl)
    }
}

@IBAction func touchphoto(_ sender: Any) {
    if let id = post?.postID {
        delegate?.goToPhotoDetail(postid: id)
    }
}
}



//profileviewcontroller
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "profilecell", for: indexPath) as! profilecell
    let post = posts[indexPath.row]
    cell.post = post
    cell.delegate = self
    return cell
}
extension ProfileViewController: profilecellDelegate {
func goToPhotoDetail(postid: String) {
    performSegue(withIdentifier: "Photo_ProfileSegue", sender: postid)
}}

0 个答案:

没有答案