1

There is a standard set of cells that have static data. However, the same UITableView has dynamic cells which are of a single prototype. Is it possible to have both static and dynamic prototype cells within a UITableView. Here is what I have tried so far:

  • Adding a UITableView within a UITableView and renaming the inner table
  • Testing in all override methods if tableView == innerTableView before customizing it

The above method is not working and even basic cell label's text is not being modified with this nesting approach.

Cœur
  • 37,241
  • 25
  • 195
  • 267
as diu
  • 1,010
  • 15
  • 31
  • 1
    I believe you'll need to set up different custom cells and use cellForRowAt to return which cells you want in the relevant indexPaths. Another possibility is to have separate sections for each type of cell. – David Henry Sep 19 '17 at 03:39
  • 1
    Having a table View inside another table view is not a good approach. What exactly do you want to achieve ? Can you show a block diagram / pic of what you would like to achieve ? – user1046037 Sep 19 '17 at 03:47
  • 1
    You cannot have both dynamic and static cells on the same table view. Use dynamic cell prototypes, but just create additional cell prototypes for those "static" cells and then have `cellForRowAt` just dequeue the cell of the appropriate reuse identifier based upon the `indexPath.row` (or whatever). – Rob Sep 19 '17 at 04:28
  • @Rob Changing a cell from Static to Dynamic does not let you reference outlets to the view controller anymore, so it's not about "_just_" doing that, it's a lot of extra work. – Cœur Jul 06 '18 at 06:44
  • 1
    Possible duplicate of [UITableView Mix of Static and Dynamic Cells?](https://stackoverflow.com/questions/18153702/uitableview-mix-of-static-and-dynamic-cells) – Cœur Jul 06 '18 at 06:45

1 Answers1

1

What I usually do in this case is to create an enum representing the type of rows that I want to display.

For example, if we want to create a To-Do List View Controller, in which we want to display two static cells: (1) "Welcome to To-Do App!" cell, (2) "Please enter your task" cell; and dynamic cells containing the to-do items, we can create an enum as follows:

enum ToDoSectionType {
    case welcome // for static cell
    case instruction // for static cell
    case tasks([Task]) // for dynamic cell

    var numberOfRows: Int {
        switch self {
        case .welcome: return 1
        case .instruction: return 1
        case let .tasks(tasks): return tasks.count
        }
    }
}

We can create a stored property in the TableView class, like

var sectionsType: [ToDoSectionType]

and assign it the correct value once we already loaded the tasks

let tasks = loadTasks()
sectionsType = [.welcome, .instruction, .tasks(tasks)]

Then in the TableViewDataSource methods, we can implement the numberOfRowsInSection and cellForRowAtIndexPath methods, like

func numberOfSections(in: UITableView) -> Int {
    return sectionsType.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionType = sectionsType[section]
    return sectionType.numberOfRows
}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
    let sectionType = sectionsType[indexPath.section]
    switch sectionType {
    case .welcome:
        let cell = tableView.dequeueReusableCell(withIdentifier: "WelcomeStaticCell")!
        return cell
    case .instruction:
        let cell = tableView.dequeueReusableCell(withIdentifier: "InstructionStaticCell")!
        return cell
    case let .tasks(tasks):
        let cell = tableView.dequeueReusableCell(withIdentifier: "DynamicTaskCell")!
        let task = tasks[indexPath.row]
        cell.textLabel?.text = task.name
        return cell
    }
}

That way, we can have a combination of static and dynamic data using only one UITableView.

ehuto
  • 11
  • 2