How to design the array structure for Multiple cells in UITableview?

uitableview multiple cell types
uitableview with multiple custom cells tutorial
swift uitableview multiple prototype cells
uitableview multiple columns swift
multiple custom cell in uitableview objective c
tableview with multiple sections swift 4
uitableview register multiple cells
uitableview inside uitableviewcell example swift

This is my first ViewController ,when I will click on plus(+) button, it will move to the SecondViewController

This is my SecondViewController, after giving the required field when I will click on save button it should add one more row in my tableview respective cell(executive,seniormanagement,staff). But it's not adding the row in respective cells.

This my code what i have tired

FirstViewController
  class TableViewHeader: UIViewController ,UITableViewDelegate,UITableViewDataSource,EmployeeProtocol{

 @IBOutlet weak var tableviewHeader: UITableView!


 var employee = [ EmployeeStatus(status:"Executive",Name:["Smith","Dev","Aryan"], Date:["Nov  14 1889","Dec  01  1980","Sep  18  1997"]),

                 EmployeeStatus(status:"SeniorManagement",Name:["Sourav","Smarak"], Date:[" April  10  1879"," Dec  11  1990"]),

                 EmployeeStatus(status:"Staff",Name:["Smith","Dev","Aryan","Suman"], Date:["Feb  14 1234"," Jan  01  1480","Sep  23  1994","July  10  1991"])]

@IBAction func plusButtonTapped(_ sender: UIButton) {
    performSegue(withIdentifier: "showSegue", sender:self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showSegue" {
        let createSegue = segue.destination as!  CreateEmployeeViewController

        createSegue.myEmpProtocol = self
    }
}

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

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return employee[section].Name.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 70
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let Label = UILabel()
    Label.text = employee[section].status
    Label.backgroundColor = UIColor.gray
    return Label
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableviewHeader.dequeueReusableCell(withIdentifier: "TableHeaderCell", for: indexPath) as! TableViewHeadercell
    cell.NameLabel.text = employee[indexPath.section].Name[indexPath.row]
    cell.DateLabel.text = employee[indexPath.section].Date[indexPath.row]
    return cell
}

func addData(status:String, Name:[String], Date:[String]) {
    employee.append(EmployeeStatus(status:status, Name:Name, Date: Date))
    tableviewHeader.reloadData()
}
}
SeconViewController
    protocol EmployeeProtocol {
        func addData(status:String, Name:[String], Date:[String])
       }

   class CreateEmployeeViewController: UIViewController {

    var myEmpProtocol:EmployeeProtocol?


@IBOutlet weak var nameTextField: UITextField!

@IBOutlet weak var birthdayTextField: UITextField!

@IBOutlet weak var StatusSegment: UISegmentedControl!


var name  = [String]()
var DOB = [String]()
var status:String = ""

@IBAction func saveButtonTapped(_ sender: UIButton) {
    self.name = [nameTextField.text!]
    self.DOB = [birthdayTextField.text!]
    if StatusSegment.selectedSegmentIndex == 0 {
        self.status = "Executive"
    } else if StatusSegment.selectedSegmentIndex == 1{
        self.status = "SeniorManagement"
    }  else if StatusSegment.selectedSegmentIndex == 2 {
        self.status = "Staff"
    }
    myEmpProtocol?.addData(status:status, Name:name, Date:DOB)

}

@IBAction func CancelButtonTapped(_ sender: UIButton) {
    dismiss(animated: true, completion: nil)
}

}
MyClass
 import Foundation
  struct EmployeeStatus {
   var status:String
   var Name:[String]
   var Date:[String]
 init(status:String, Name:[String], Date:[String]){
    self.status = status
    self.Name = Name
    self.Date = Date
   }

Your current code is adding the new EmployeeStatus to employee array, not the item's EmployeeStatus.Name array and EmployeeStatus.Date array.

Please try this code.

func addData(status:String, Name:[String], Date:[String]) {
    var arr = [EmployeeStatus]()
    employee.forEach { ele in
        if ele.status == status {
            var e = ele
            e.Name.append(Name[0])
            e.Date.append(Date[0])
            arr.append(e)
        } else {
            arr.append(ele)
        }
    }
    employee = arr
    tableviewHeader.reloadData()
}

How to design the array structure for Multiple cells in UITableview?, Your current code is adding the new EmployeeStatus to employee array, not the item's EmployeeStatus.Name array and EmployeeStatus.Date array. Please try  The only property the ViewModel will have is the array of items, that will represent the array of sections for the UITableView: class ProfileViewModel: NSObject { var items = [ProfileViewModelItem

Your data looks interesting. It might work better with

enum Level: Int {
    case executive = 0, seniorManagement, staff
    var stringValue: { return (String:description: self).capitalizingFirstLetter }
}
struct Employee {
    let name: String
    let date: String
}

var employees: [Level: [Employee]] = [
    .executive: [Employee(name: "Smith", date: "Nov  14 1889"],
    .seniorManagement: [],
    .staff: []]

Then you can add employees with:

guard let level = Level(rawValue: status) else {
    throw Errors.levelDoesNotExist(status)
}
employees[.level] = newEmployee

Reloading the tableView works as you do already. Just the data is in a format where its weird to add to.

And best dismiss the second viewController once its finished after the addData delegate call so its going back to the first to show the updated data.

iOS: How to build a Table View with multiple cell types, The are also Table Views with dynamic cells that have different content types: the If you change one index, the whole table view structure will be broken and First, create a new project, add a TableView to default ViewController, pin the is the array of items, that will represent the array of sections for the UITableView: UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want. You can also supply header and footer views to

You have to set your view controller as a table view delegate and datasource.

Add to the end of the viewDidLoad at TableViewHeader class:

override func viewDidLoad() {
    super.viewDidLoad()

    tableviewHeader.delegate = self
    tableviewHeader.dataSource = self
}

Dealing with Complex Table Views in iOS and Keeping Your Sanity, Add to that array index math and the occasional, very fun, out of bounds error and about here: the table view has different states, and multiple cells and sections. cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView. Just a regular iOS developer making some table views struct Section { 1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to tableview with sections & save it on desktop. 2. Go to main storyboard & select view controller & delete it. Add tableview controller in main storyboard. 3. Select tableview controller & make it initial view controller 4.

In this way, you can pass value from one view controller to another tab:

  1. First of all open main.storyboard
  2. Select the First view controller and drag and drop UItextField and UIButton on the field.

Filling a Table with Data, Create and configure cells for your table dynamically using a data source to structure your data in a way that makes it easy to retrieve the row and section In this table, each row displays a string, so the implementation stores an array A cell is a UITableViewCell object that acts like a template for the rows of your table​. By this video you can learn how to create tableview with multiple custom cell. In this video , I have two custom cell with in single tableview. This tutorial will be very helpful for iOS developer

Grouping UITableView cells into sections, This tutorial shows how to group UITableView cells into sections. It's also an introduction by example on how to create generic types for abstracting common Define a struct type to store the Headlines grouped by month in StoriesTableViewController.swift: Map the result to an Array of MonthSections:. We create the cell by using a method of UITableView. We have to hand over the so-called reusable identifier, which we have specified in the interface builder before. The table view manages the creation and reusing of table view cells. We will talk about the details later. We look up the data item that corresponds to the indexPath.

UITableViewController - iOS & Swift Tutorial, Table View with data from Swift Array Using Custom cells to customize UITableViewCells Making table cells reorderable: Show the Document Outline and familiarize yourself with the object structure. There are four different types of cell styles that have views out of the box - Basic, Left/Right Detail  This demo explains you how to use multiple prototype cells in a single UITableView with storyboard in 5 minutes. Use mutiple prototye cells in UITableView with storyboard How to create

Structure your UITableView better with structs and enums in Swift , Structure your UITableView better with structs and enums in Swift would be to create an internal data structure for your UITableViewController quote and the currency rate if the stock is quoted in a currency different from yours. So when you tap on a cell of the stock list, you get pushed to another Table  Select the top one and use a Subtitle cell style named cell as the identifier. Change the background color to #FFEEEE in the color picker. Select the bottom one and make it a Right Detail cell named options cell as the identifier. Change the Background color to #EEFFFF. Set Up the Basic Table View. Now go to the ViewController.swift file. Change the class name to this:

Comments
  • In your secondViewController where are you calling your custom delegate method?
  • show your second page code
  • Show your SecondViewController code
  • Use lowerCamelCase for variables and arguments swift.org/documentation/api-design-guidelines
  • @AbecedarioPoint yes i have edited my code now you can check
  • i think ,i am reloading tableview in a wrong place ,not sure
  • @Laxmipriya Put a breakpoint at addData method to check if it has been called and the new item has been added into employee.
  • yes addData() is calling fine and i am getting value
  • @Laxmipriya You can avoid it by structuring your data differently. A data structure is to help make operations easy, and your shown structure makes adding employees to it complicated.
  • @NF Lee Thank you very much
  • I did already, look at what I wrote. Have you tried integrating that?