Giter Site home page Giter Site logo

mohsinalimat / ascollectionview-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apptekstudios/ascollectionview

1.0 2.0 0.0 3.45 MB

A SwiftUI port of UICollectionView with support for custom layouts, preloading, and more.

License: MIT License

Swift 100.00%

ascollectionview-1's Introduction

Contributors Forks Stargazers Issues MIT License

ASCollectionView

A SwiftUI implementation of UICollectionView & UITableView. Here's some of its useful features:

  • supports preloading and onAppear/onDisappear.
  • supports removing separators (for tableview).
  • supports autosizing of cells
  • supports the new UICollectionViewCompositionalLayout, and any other UICollectionViewLayout

Pull requests and suggestions welcome :)

Report Bug · Suggest a feature

Screenshots from demo app

Table of Contents

Getting Started

ASCollectionView is a swift package.

Usage

Below is an example of how to include a collection view with two sections (each with their own data source). For an extended example with a custom compositional layout see here. Or for more in-depth examples download the demo project included in this repo.

import SwiftUI
import ASCollectionView

struct ExampleView: View {
    @State var dataExampleA = (0 ..< 21).map { $0 }
    @State var dataExampleB = (0 ..< 15).map { "ITEM \($0)" }
    
    typealias SectionID = Int
    
    var layout: ASCollectionViewLayout<SectionID> {
        ASCollectionViewLayout { sectionID -> ASCollectionViewLayoutSection in
            switch sectionID {
            case 0:
                // Here we use one of the predefined convenience layouts
                return ASCollectionViewLayoutGrid(layoutMode: .adaptive(withMinItemSize: 100), itemSpacing: 5, lineSpacing: 5, itemSize: .absolute(50))
            default:
                return self.customSectionLayout
            }
        }
    }
    
    var body: some View
    {
        ASCollectionView(layout: self.layout) {
            ASCollectionViewSection(id: 0,
                                    data: dataExampleA,
                                    dataID: \.self) { item in
                                        Color.blue
                                            .overlay(
                                                Text("\(item)")
                                        )
            }
            ASCollectionViewSection(id: 1,
                                    data: dataExampleB,
                                    dataID: \.self) { item in
                                        Color.blue
                                            .overlay(
                                                Text("Complex layout - \(item)")
                                        )
            }
            .sectionHeader {
                HStack {
                    Text("Section header")
                        .padding()
                    Spacer()
                }
                .background(Color.yellow)
            }
            .sectionFooter {
                Text("This is a section footer!")
                    .padding()
            }
        }
    }
    
    let customSectionLayout = ASCollectionViewLayoutCustomCompositionalSection { (layoutEnvironment, _) -> NSCollectionLayoutSection in
        // ...
        // Your custom compositional layout section here. For an example see this file: /readmeAssets/SampleUsage.swift
        // ...
        return section
    }
}

Layout

  • There is inbuilt support for the new UICollectionViewCompositionalLayout.
    • You can define layout on a per-section basis, including the use of a switch statement if desired.
    • There are some useful structs (starting with ASCollectionViewLayout...) that allow for easy definition of list and grid-based layouts (including orthogonal grids).

Other tips

  • You can use an enum as your SectionID (rather than just an Int), this lets you easily determine the layout of each section.
  • See the demo project for more in-depth usage examples.
  • Please note that you should only use @State for transient visible state in collection view cells. Anything you want to persist long-term should be stored in your model.

Todo

See the open issues for a list of proposed features (and known issues).

License

Distributed under the MIT License. See LICENSE for more information.

ascollectionview-1's People

Contributors

apptekstudios avatar

Stargazers

MohsinAli avatar

Watchers

MohsinAli avatar  avatar

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.