Jimmy James
Jimmy James

Reputation: 847

UICollectionView section border in Swift

I'm trying to use UICollectionView to bind different cells with an XIB file and set a design for it.

I know how to bind different cells and it's works fine in my application.

Here the code to bind:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    var cell : UICollectionViewCell
    mCurrentIndexPath = indexPath

    // HEADER
    switch indexPath.section {
    case 0:
        cell = configureModuleHeaderCell(indexPath)
    default:
        // Local
        let theme = getThemeFromIndex(indexPath.section - 1)
        mCurrentDocuments = getDocumentsFromTheme(theme)
        let cours : DownloadableDocument? = (mCurrentDocuments != nil) ? getCoursForTheme() : nil
        mCurrentDocuments = deleteCoursFromDocAnnexe()
        mCurrentDocuments = sortDocumentDoublePDF()
        if indexPath.row == 0 {
            cell = configureThemeHeaderCell(theme, cours: cours)
        }

            // NORMAL
        else {
            cell = configureThemeDocCell()
            cell.layer.borderWidth = 1.0
            cell.layer.borderColor = UIColor.grayColor().CGColor
        }
        break
    }

    return cell
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    if mFetchedResultController != nil {
        mCurrentIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        return mFetchedResultController!.fetchedObjects!.count + 1
    }
    return 0
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    var counter : Int = 1
    switch section {
    case 0:
        counter = 1
        break
    default:
        mCurrentDocuments = getDocumentsFromTheme(getThemeFromIndex(section - 1))
        mCurrentDocuments = checkDoubleCours()
        counter = (mCurrentDocuments != nil) ? mCurrentDocuments!.count : 0
        counter += (!documentsContainsCours(mCurrentDocuments)) ? 1 : 0
        break
    }
    return counter
}

Then I want to set a border for each section. Is that possible ?

I can set a border for cell by :

cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.grayColor().CGColor

But I want to do it for a section.

Upvotes: 2

Views: 2660

Answers (1)

Jimmy James
Jimmy James

Reputation: 847

I've finally found a solution !

I just need to calculate the height of all cells of a section, set this height on the view and position it well.

EDIT: Update for @Urmi

Add this function to instanciate a cardView with frame

func instanciateCardView(frame : CGRect) -> UIView {
    // Declaration
    let view = UIView(frame : frame)

    // Basic UI
    view.backgroundColor = UIColor.whiteColor()
    view.layer.backgroundColor = UIColor.whiteColor().CGColor
    view.layer.borderWidth = 1.0
    view.layer.borderColor = ColorsUtil.UIColorFromRGB(0xAB9595).CGColor

    // Shadow (cardview style)
    view.layer.shadowPath = UIBezierPath(rect: view.bounds).CGPath
    view.layer.shouldRasterize = true
    view.layer.shadowColor = ColorsUtil.UIColorFromRGB(0xD5C6C6).CGColor
    view.layer.shadowOpacity = 1
    view.layer.shadowOffset = CGSizeMake(1, 1);
    view.layer.shadowRadius = 1.0

    if !mCardViewList.contains(view) {
        // Add view to container - Just to save the instance
        mCardViewList.append(view)

        // Add view to main view
        self.collectionView?.addSubview(view)
        self.collectionView?.sendSubviewToBack(view)
    }

    return view
}

And in the cellForItemAtIndexPath :

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell : UICollectionViewCell
    mCurrentIndexPath = indexPath
    switch indexPath.section {
    case 0:
        cell = configureModuleHeaderCell(indexPath)

        // Basic UI
        cell.backgroundColor = UIColor.whiteColor()
        cell.layer.borderWidth = 1.0
        cell.layer.borderColor = ColorsUtil.UIColorFromRGB(0xAB9595).CGColor

        // Shadow (cardview style)
        cell.layer.shadowPath = UIBezierPath(rect: view.bounds).CGPath
        cell.layer.shouldRasterize = true
        cell.layer.shadowColor = ColorsUtil.UIColorFromRGB(0xD5C6C6).CGColor
        cell.layer.shadowOpacity = 1
        cell.layer.shadowOffset = CGSizeMake(1, 1);
        cell.layer.shadowRadius = 1.0

        break
    default:
        let theme = getThemeFromIndex(indexPath.section - 1)
        mCurrentDocuments = getDocumentsFromTheme(theme)
        let cours : DownloadableDocument? = (mCurrentDocuments != nil) ? getCoursForTheme() : nil
        mCurrentDocuments = deleteCoursFromDocAnnexe()
        mCurrentDocuments = sortDocumentDoublePDF(mCurrentDocuments)

        if indexPath.row == 0 {
            cell = configureThemeHeaderCell(theme, cours: cours)
            if mCurrentDocuments != nil {
                if mCurrentDocuments!.count == 0 {
                    instanciateCardView(CGRectMake(cell.frame.origin.x - 30, cell.frame.origin.y - 10, cell.frame.width + 60, cell.frame.height + 20))
                }
            }
        }
        else {
            cell = configureThemeDocCell()
            if indexPath.row == mCollectionView.numberOfItemsInSection(indexPath.section) - 1 {

                // Attribute
                let index = NSIndexPath(forRow: 0, inSection: indexPath.section)
                let headerAttribute : UICollectionViewLayoutAttributes = mCollectionView.layoutAttributesForItemAtIndexPath(index)!
                let height = (cell.frame.origin.y + cell.frame.height) - (headerAttribute.frame.origin.y - 10)

                instanciateCardView(CGRectMake(headerAttribute.frame.origin.x - 30, headerAttribute.frame.origin.y - 10, headerAttribute.frame.width + 60, height + 20))
            }
        }
        break
    }

    return cell
}

I've found that is not the best solution, but it works. Now i've declare a UITableView and add a UICollectionView in each UITableViewCell

Don't forget to upvote if it helps you :)

Upvotes: 1

Related Questions