zhxnlai / ZLSwipeableViewSwift

A simple view for building card like interface inspired by Tinder and Potluck.
MIT License
2.25k stars 228 forks source link

Unable to integrate this with UICollectionView. #122

Closed api42 closed 6 years ago

api42 commented 6 years ago

Just started using it. It works really good. But here is an issue I'm facing.

I'm using uicollectiionview.

I did all the setup required.

in my controller file

func nextCardView() -> UIView? {

        let cardView = MyCollectionView(frame: swipeableView.bounds)
        cardView.backgroundColor = colorForName(colors[colorIndex])

        if loadCardsFromXib {
            let contentView = Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)?.first! as! UIView
            contentView.translatesAutoresizingMaskIntoConstraints = false
            contentView.backgroundColor = cardView.backgroundColor
            cardView.addSubview(contentView)

            constrain(contentView, cardView) { view1, view2 in
                view1.left == view2.left
                view1.top == view2.top
                view1.width == cardView.bounds.width
                view1.height == cardView.bounds.height
            }
        }
        return cardView
    }

This, of course, loads the UI from mycollectionview but without data. Obviously, I'm doing something wrong.

This is somewhat how mycollectionview looks like

class PostUi: UICollectionViewCell {
    var post: Post? {
        didSet {
            guard let title = post?.title else { return }
        }
    }

    lazy var titleLabel: UITextView = {
        let textView = UITextView()
        textView.font = UIFont.boldSystemFont(ofSize: 16)
        textView.isUserInteractionEnabled = true
        textView.sizeToFit()
        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints = true
        textView.isEditable = false

        textView.layoutIfNeeded()
        let tap = UITapGestureRecognizer(target: self, action: #selector(titleLabelTapped))
        tap.numberOfTapsRequired = 1
        textView.addGestureRecognizer(tap)

        return textView
    }()

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

        addSubview(titleLabel)

        titleLabel.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: -3, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)    

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
AndrewSB commented 6 years ago

@apurva2342: I'd recommend first creating a SwipeableView outside of collection view, once you have that working then try putting it into a collectionView.

That way you can further isolate where the problem actually is: simply with using the swipeable view, or the interaction b/w the collection view & swipeable view

api42 commented 6 years ago

I don't think that's the issue. The issue is I get the views but not the data.

I'm probably making a very noob mistake.

in my collectionveiw controller This is what I usually do.

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: postCellId, for: indexPath) as! PostUi

        cell.post = posts[indexPath.item]
        cell.delegate = self

        return cell
    }

But no idea how to use your classes with this. How to correctly dequeue cell and populate those cards?

AndrewSB commented 6 years ago

What are you trying to achieve? Just one stack of cards? Or many stacks of cards? If you're just trying to get one card, you dont need to use collectionView at all. Look at the demo and it has an example

api42 commented 6 years ago

No. It's many stack of cards.

AndrewSB commented 6 years ago

start by creating one stack of cards in a UIView, then embed that UIView inside the UICollectionViewCell (in your case the class you've named PostUi)

AndrewSB commented 6 years ago

Closing due to inactivity