joelekstrom / JEKScrollableSectionCollectionViewLayout

A UICollectionView flow layout with individually scrollable sections
MIT License
73 stars 16 forks source link

Horizontal scroll for particular section #21

Closed RohithPsingh closed 4 years ago

RohithPsingh commented 4 years ago

Hi i have 9 sections totally, need to scroll horizontal for 3 sections. in the below delegate method i added these two lines., but still it doesn't work

collectionView.isPagingEnabled = true layout.defaultScrollViewConfiguration.isPagingEnabled = true

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

If i add the below code in viewDidLoad its reflecting to all sections.

let layout = JEKScrollableSectionCollectionViewLayout() dashBoardCollectionView.collectionViewLayout = layout

can you please help me out from this., please share some sample code(swift) in detail with these possibilities..

Thanks

joelekstrom commented 4 years ago

Hi!

To achieve this, your controller class should conform to JEKCollectionViewDelegateScrollableSectionLayout, and then implement the following method. This example disables scrolling for section 2:

func collectionView(_ collectionView: UICollectionView, layout: JEKScrollableSectionCollectionViewLayout, scrollViewConfigurationForSection section: UInt) -> JEKScrollViewConfiguration? {
    let config = JEKScrollViewConfiguration.default()
    if section == 2 {
        config.isScrollEnabled = false
    }
    return config;
}

You can customize each section in several ways using this method.

RohithPsingh commented 4 years ago

Hi!

To achieve this, your controller class should conform to JEKCollectionViewDelegateScrollableSectionLayout, and then implement the following method. This example disables scrolling for section 2:

func collectionView(_ collectionView: UICollectionView, layout: JEKScrollableSectionCollectionViewLayout, scrollViewConfigurationForSection section: UInt) -> JEKScrollViewConfiguration? {
    let config = JEKScrollViewConfiguration.default()
    if section == 2 {
        config.isScrollEnabled = false
    }
    return config;
}

You can customize each section in several ways using this method.

Thanks much.,its working fine.. i have one more query. i have few sections need to display full array count in vertical. like i have 8 cells in one section need to show in vertical, but here its showing in horizontal with 3 cells in screen without scroll. which delegate method we need to use . please help..

joelekstrom commented 4 years ago

Ah, I think you want to use a flow layout in those sections. Try this method instead:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {
    if section == 2 {
        return true
    }
    return false
}

This way, the whole section should layout like a standard flow layout, meaning that if there are more cells than fit horizontally it will add several rows instead.

RohithPsingh commented 4 years ago

Ah, I think you want to use a flow layout in those sections. Try this method instead:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {
    if section == 2 {
        return true
    }
    return false
}

This way, the whole section should layout like a standard flow layout, meaning that if there are more cells than fit horizontally it will add several rows instead.

i tried,but its not working., can you give explanation in detail..

Ex: for suppose i have 4 sections, two need to scroll horizontal and two not to scroll for this i used first method: section-1 & 3 is scrollable thats perfect . but remaining two sections also coming in one row i.e, filled with three cells per row.. i need to show actual count in vertical for remaining

as per your suggestion i tried the second method, but its not working.. looks like i am wrong somewhere. please correct me.

pls check the code below:

extension ViewController:UICollectionViewDelegateFlowLayout,JEKCollectionViewDelegateScrollableSectionLayout { func collectionView(_ collectionView: UICollectionView, layout: JEKScrollableSectionCollectionViewLayout, scrollViewConfigurationForSection section: UInt) -> JEKScrollViewConfiguration? { let config = JEKScrollViewConfiguration.default() if section == 0 { config.isScrollEnabled = false } else if section == 2 { config.isScrollEnabled = false } return config; }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool { if section == 0 { return true } else if section == 2 { return true } return false }

}

please help me and give me some detail explanation.

Thanks

joelekstrom commented 4 years ago

So you only want a single cell per row in those sections if I understand correctly?

In that case, try playing with interItemSpacing for those sections. It’s used in one of the sections of the example project. Make the spacing large enough that only one item fits per row.

I think if you could provide an image of the layout you need I can help you more easily

joelekstrom commented 4 years ago

Yeah, it looks like you want shouldUseFlowLayout for those sections. But I don't think you should disable scrolling for them actually.

Try removing the func collectionView(_ collectionView: UICollectionView, layout: JEKScrollableSectionCollectionViewLayout, scrollViewConfigurationForSection section: UInt) -> JEKScrollViewConfiguration?, because what you're trying to isn't actually disabling scrolling. You're trying to use flow layout. Maybe they're incompatible with eachother? Are you sure that shouldUseFlowLayoutInSection is called? Try setting a breakpoint.

To clarify, the only delegate code that should be needed is this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {
    if section == 0 {
        return true
    }
    else if section == 2 {
        return true
    }
    return false
}
joelekstrom commented 4 years ago

As an example, here's a section in the example project using flow layout, which should be exactly what you need.

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-08-21 at 22 09 44

RohithPsingh commented 4 years ago

As an example, here's a section in the example project using flow layout, which should be exactly what you need.

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-08-21 at 22 09 44

this is what exactly i am looking for., i saw your example its in objective-c.. it would be nice and helpful if you add swift example also..

Thank u

RohithPsingh commented 4 years ago

Yeah, it looks like you want shouldUseFlowLayout for those sections. But I don't think you should disable scrolling for them actually.

Try removing the func collectionView(_ collectionView: UICollectionView, layout: JEKScrollableSectionCollectionViewLayout, scrollViewConfigurationForSection section: UInt) -> JEKScrollViewConfiguration?, because what you're trying to isn't actually disabling scrolling. You're trying to use flow layout. Maybe they're incompatible with eachother? Are you sure that shouldUseFlowLayoutInSection is called? Try setting a breakpoint.

To clarify, the only delegate code that should be needed is this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {
    if section == 0 {
        return true
    }
    else if section == 2 {
        return true
    }
    return false
}

i tried this delegate method alone its not working. i tried with breakpoint also,its not even calling into this method.. please help me., how to make these kind of view..

Thanks

joelekstrom commented 4 years ago

Right in the bottom of the file Tests.swift there's an example of this delegate method in Swift.

The signature is func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {

If that's never called in your delegate, I think I need to see your whole controller class to find the error. Make sure you implemented it in the object set as collectionView.delegate.

RohithPsingh commented 4 years ago

Right in the bottom of the file Tests.swift there's an example of this delegate method in Swift.

The signature is func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {

If that's never called in your delegate, I think I need to see your whole controller class to find the error. Make sure you implemented it in the object set as collectionView.delegate.

i tried with all the possibilities., still i didnt get the right solution.., shouldUseFlowLayoutInSection delegate method is never called. scrollViewConfigurationForSection this delegate method is calling.

is the below lines mandatory to call., i wrote this two lines in viewDidLoad() let layout = JEKScrollableSectionCollectionViewLayout() dashBoardCollectionView.collectionViewLayout = layout

pls let me know if i need to correct anywhere..

Thanks

RohithPsingh commented 4 years ago

Right in the bottom of the file Tests.swift there's an example of this delegate method in Swift.

The signature is func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: JEKScrollableSectionCollectionViewLayout, shouldUseFlowLayoutInSection section: Int) -> Bool {

If that's never called in your delegate, I think I need to see your whole controller class to find the error. Make sure you implemented it in the object set as collectionView.delegate.

delegate and datasource is configured through sotryboard., how to implement this below line : "Make sure you implemented it in the object set as collectionView.delegate."

Thanks

joelekstrom commented 4 years ago

That should be OK. And since the other delegate method is called, I think it's wired up correctly. Very strange that this one isn’t called. Feels like it might be spelled incorrectly or something then? Since the tests pass (and example project works) the library itself should be doing the correct thing. Anyway, it's hard for me to help unless you could post your whole controller class

RohithPsingh commented 4 years ago

That should be OK. And since the other delegate method is called, I think it's wired up correctly. Very strange that this one isn’t called. Feels like it might be spelled incorrectly or something then? Since the tests pass (and example project works) the library itself should be doing the correct thing. Anyway, it's hard for me to help unless you could post your whole controller class

how to share class file to u., can u provide any email or any other way.?

joelekstrom commented 4 years ago

Send it to jek@ekstrom.io

RohithPsingh commented 4 years ago

Thanks much bro., issue resolved..