Giter Club home page Giter Club logo

Comments (6)

alexdrone avatar alexdrone commented on May 13, 2024

UICollectionView works a bit differently from UITableView - you need to register a class for a reuse idenfier:

collectionView.registerClass(ComponentCollectionViewCell<MyComponent>.self, forCellWithReuseIdentifier: CellPrototype.defaultIdentifier(MyComponent.self))

Also check out Buffer - it has built-in table and collection view adapters that work great with Render.

from render.

ranhsd avatar ranhsd commented on May 13, 2024

Thanks! it works. I tried with Buffer and wanted to know. Can i create a sub state and send it into Buffer in my app?

from render.

alexdrone avatar alexdrone commented on May 13, 2024

from render.

ranhsd avatar ranhsd commented on May 13, 2024

ok. I will try to use it again. Do you know how you can create collection view cell on full screen? currently i am trying to do it but it is only fixed size?

from render.

alexdrone avatar alexdrone commented on May 13, 2024

You can set the width and the height of the cell to the size parameter passed in the closure.

from render.

jewilhel avatar jewilhel commented on May 13, 2024

Hi Ran or Alex,
I'm trying to setup a collection view component and I was looking at the code snippets above trying to piece one together, but I'm still having trouble. I'm not sure what the issue is. The collection view draws to screen, but neither of my data source methods are called and no cells are rendered. I'm trying to do this purely in code so not using storyboards for either the collection view or cell prototype. Here is the code I have so far:

import Foundation
import UIKit
import Render

struct CollectionState: StateType {
    var id: String = "collectionContainer"
    var elements: [NodeType] = [layoutBuilder("")]
    var layoutAs: String = "flow"
    var direction: String = "vertical"
}

class CollectionContainer: ComponentView<CollectionState>, UICollectionViewDataSource, UICollectionViewDelegate {
    
    var collectionView: UICollectionView?
    
    required init() {
        super.init()
        //self.defaultOptions = [.preventViewHierarchyDiff]
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("Not supported")
    }
    
    override func render() -> NodeType {
        // A container view that wraps it's contents
        func layoutContainer() -> NodeType {
            return Node<UICollectionView>(
                create: {[weak self] in
                    let layout = UICollectionViewFlowLayout()
                    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
                    collectionView.register(ComponentCollectionViewCell.self, forCellWithReuseIdentifier: CellComponent.reuseIdentifier)
                    collectionView.dataSource = self
                    collectionView.delegate = self
                    collectionView.backgroundColor = UIColor.blue
                    collectionView.isPagingEnabled = true
                    collectionView.showsHorizontalScrollIndicator = false
                    collectionView.showsVerticalScrollIndicator = false
                    collectionView.alwaysBounceHorizontal = true
                    self?.collectionView = collectionView
                    return collectionView
                },
                configure: {(collection,layout,size) in
                    layout.alignSelf = .stretch
                    layout.width = size.width
                    layout.height = size.height
                    configs[self.state.layoutAs]!(layout)
                    configs[self.state.direction]!(layout)
            }
            )}
        
        return layoutContainer()
    }
    
    // tell the collection view how many cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("Count")
    return state.elements.count
    }
    
    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let id = CellComponent.reuseIdentifier
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: id, for: indexPath)
        
        if let cell = cell as? ComponentCollectionViewCell {
            cell.mountComponentIfNecessary(CellComponent())
            cell.set(state: CellState(node: self.state.elements[indexPath.row]))
            print("Add Cells")
            cell.update(options: [])
        }
        return cell
    }
}

from render.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.