# How to Enable Irregular Cell Size Calculation in JJCollectionViewRoundFlowLayout

> Enable irregular cell size calculation in JJCollectionViewRoundFlowLayout by setting isCalculateTypeOpenIrregularitiesCell to YES. Customize your collection view layout effortlessly.

- Repository: [Kingjiajie_Go/jjcollectionviewroundflowlayout](https://github.com/kingjiajie/jjcollectionviewroundflowlayout)
- Tags: how-to-guide
- Published: 2026-03-05

---

**Set the `isCalculateTypeOpenIrregularitiesCell` property to `YES` on your `JJCollectionViewRoundFlowLayout` instance before the collection view performs its first layout pass.**

The `kingjiajie/jjcollectionviewroundflowlayout` repository provides rounded section backgrounds for `UICollectionView`. When you need **irregular cell size calculation**, the library computes a union rectangle that tightly encloses cells of varying dimensions instead of assuming a uniform grid, ensuring backgrounds and borders render correctly around heterogeneous items.

## Understanding the `isCalculateTypeOpenIrregularitiesCell` Property

The layout engine exposes a Boolean control flag in [`JJCollectionViewRoundFlowLayout.h`](https://github.com/kingjiajie/jjcollectionviewroundflowlayout/blob/main/JJCollectionViewRoundFlowLayout.h):

```objc
@property (nonatomic, assign) BOOL isCalculateTypeOpenIrregularitiesCell;

```

By default, this property is **NO**, indicating the layout expects uniform cell sizes. When you set it to **YES**, the implementation in `JJCollectionViewRoundFlowLayout.m` inspects the actual `frame` of every cell during `prepareLayout` and `layoutAttributesForElementsInRect:`. It builds a union rect that encloses all cells within each section, then uses that geometry to draw the rounded background, borders, and shadows defined in `JJCollectionViewRoundConfigModel`.

## Implementation Steps

Follow these steps to activate irregular cell sizing:

1. **Initialize the layout.** Create an instance of `JJCollectionViewRoundFlowLayout` programmatically or retrieve it from your storyboard.
2. **Enable the flag.** Set `isCalculateTypeOpenIrregularitiesCell = YES` immediately after initialization.
3. **Provide per-item sizes.** Implement `collectionView:layout:sizeForItemAtIndexPath:` in your delegate to return non-uniform sizes.
4. **Configure supplementary views.** Adjust related properties such as `isCalculateHeader` or `isCalculateFooter` if your section includes headers or footers.
5. **Assign the layout.** Attach the configured layout to your `UICollectionView` before adding it to the view hierarchy.

## Code Example

The following Objective-C snippet demonstrates the complete setup:

```objc
#import "JJCollectionViewRoundFlowLayout.h"

- (void)setupCollectionView {
    // 1. Create the layout
    JJCollectionViewRoundFlowLayout *layout = [[JJCollectionViewRoundFlowLayout alloc] init];
    
    // 2. Enable irregular-cell calculation
    layout.isCalculateTypeOpenIrregularitiesCell = YES;
    
    // Optional: enable background calculation for headers/footers
    layout.isCalculateHeader = YES;
    layout.isCalculateFooter = YES;
    
    // 3. Attach to collection view
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
                                                          collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
}

// Delegate method providing heterogeneous sizes
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item % 2 == 0) {
        return CGSizeMake(120, 80);  // Wide cell
    } else {
        return CGSizeMake(80, 120);  // Tall cell
    }
}

```

## Key Source Files

According to the `kingjiajie/jjcollectionviewroundflowlayout` source code, the irregular cell calculation logic resides in these files:

- **[`JJCollectionViewRoundFlowLayout.h`](https://github.com/kingjiajie/jjcollectionviewroundflowlayout/blob/main/JJCollectionViewRoundFlowLayout.h)** – Declares the `isCalculateTypeOpenIrregularitiesCell` property and other layout-control flags.
- **`JJCollectionViewRoundFlowLayout.m`** – Implements the layout logic that checks this flag during `prepareLayout` to determine whether to compute union rects from actual cell frames.
- **[`JJCollectionViewRoundConfigModel.h`](https://github.com/kingjiajie/jjcollectionviewroundflowlayout/blob/main/JJCollectionViewRoundConfigModel.h)** – Defines the visual configuration model (corner radius, border width, shadow, background color) applied to the calculated union rect.

## Summary

- **Enable irregular cell size calculation** by setting `layout.isCalculateTypeOpenIrregularitiesCell = YES`.
- **Set the flag early**, before the collection view's first layout pass, to ensure the union rect calculation runs in `prepareLayout`.
- **The layout reads actual cell frames** from `UICollectionViewDelegateFlowLayout` when the flag is active, creating a tight-fitting background for heterogeneous sections.
- **Configuration models** in `JJCollectionViewRoundConfigModel` control the visual appearance of the calculated background.

## Frequently Asked Questions

### When should I set `isCalculateTypeOpenIrregularitiesCell`?

Set the property immediately after creating the layout instance and before assigning it to the collection view. The layout reads this value during `prepareLayout`, which runs when the collection view first appears or when you call `invalidateLayout`.

### Does enabling this property affect performance?

Yes, calculating union rects for irregular cells requires iterating through every cell frame in the section during the layout pass. For collections with thousands of items, consider using uniform cell sizes or enabling the flag only for sections that truly require irregular backgrounds.

### Can I use this feature with self-sizing cells?

The property works best with manually provided sizes via `collectionView:layout:sizeForItemAtIndexPath:`. Self-sizing cells using `UICollectionViewCell` auto-layout may produce unpredictable background rects because the layout calculates the union before final cell sizes are determined. Pin your cell sizes in the delegate method for reliable results.

### What visual styles apply to the irregular background?

Any configuration set in `JJCollectionViewRoundConfigModel` applies to the calculated union rect, including `cornerRadius`, `borderWidth`, `borderColor`, `shadow`, and `backgroundColor`. The layout creates the background view to match the irregular shape, then applies these properties.