Andre
Andre

Reputation: 7638

iOS Swift: CollectionView inside TableView with multiple sections

I have the following design:

A table with multiple sections and multiple rows for each section. For each row I have a collection view.

I'm having a problem to assign delegates to the collections because of the multiple table sections.

How can I use the collection.tag to refer to a specific section and row of the table view?

Upvotes: 2

Views: 438

Answers (2)

Abhishek Thapliyal
Abhishek Thapliyal

Reputation: 3708

Better Approach is to user Section Models

struct CellModel {
   var collectionModel: [ModelType] = []
}

struct SectionModel {
    headerModel: Any
    cellModels: [CellModel] = []
}


var sections: [SectionModel] = []

// intialize array some where or API Response

in UITabelView delegate method

number of rows

return self.sections[section].cellmodels.count

number of sections will be

return self.sections.count

in cell for row at index

cell.cellModels = self.sections[indexpath.section].cellModels[indexpath.row]

In CellCustomClass

declare var cellModels: [ModelType] = []

Now TableCell consist collection view You can use this array as it data source

Upvotes: 0

BXV
BXV

Reputation: 423

A better approach would be

  1. Create a view controller to hold Table View
  2. Create a table view cell to hold collection view
  3. Create a collection view cell

And connect the table view's delegate to view controller and connect the collection view's delegate to the table view cell.

Upvotes: 1

Related Questions