ios - UITableview中Multiple cells的数组结构如何设计?

标签 ios swift uitableview uiviewcontroller

这是我的第一个 ViewController ,当我点击加号 (+) 按钮时,它会移动到 SecondViewController

enter image description here

这是我的 SecondViewController,在我单击保存按钮时提供必填字段后,它应该在我的 tableview 相应单元格(执行、高级管理、职员)。但它不会在相应的单元格中添加行。

enter image description here

这是我的代码,我已经厌倦了

第一 View Controller

  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)
}

}

我的类(class)

 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
   }

最佳答案

您当前的代码是将新的 EmployeeStatus 添加到 employee 数组,而不是项目的 EmployeeStatus.Name 数组和 EmployeeStatus.Date 数组。

请尝试此代码。

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()
}

关于ios - UITableview中Multiple cells的数组结构如何设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51320326/

相关文章:

ios - 使用编译器条件来控制委托(delegate)实现?

ios - 如何将泛型类型指定为变量?

ios - (Swift) 如何在字符串中打印 "\"字符?

ios - 完整深入的 switch 语句故障

ios - 是 insertSections :withRowAnimation: necessary? 的显式调用

ios - 在 Swift 运行时确定实例是否具有可设置的属性

ios - 使用 Interface Builder 中的自动布局而不是代码使 UIView 填充包含 View

ios - 将 UITapGesture 添加到 UIScrollView

ios - Swift 中的事件指示器

ios - 在 UITableViewController 的 UINavigationItem 栏中从 "Done"更改后按下 "Edit"时触发什么?