-1

I would like to know how to collect data from previous page when using UITableView.

It's hard to explain so I will give you an example.

Apple default calendar app has this feature in the New Event Page. When you open the New Even page, you will see Never in Repeat field. To change this, you need to tap Never and go to the next page and select something like Every Week. If you select Every Week, it will go back to the first page and the Repeat field now shows Weekly.

I would like to make something similar but not sure how to set this up... My questions are; Do I need to use Segue? Do I need to use UITextField or UILabel for the cell? What triggers to pass the data?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Riki_U
  • 1
  • 1

1 Answers1

0

MainviewController to add Below code

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // "SelectionSegue" is same as storyboard segue identifier
    if segue.identifier == "SelectionSegue" {
        (segue.destination as! SelectCategoryViewController).selectedCategoryValue = self.selectedCategoryLabel.text!
        print(selectedCategoryLabel)
    }
}

Set Segue Identifier in Storyboard

enter image description here

SelectedTableView

var selectedCategoryValue:String = ""
var CategeryArray = ["Food","Travel","Shopping","Card","MyReserve","Game","Songs","Movies","Entertainment","Business","Education","Finance","Drink","Sports","Social","Lifestyle"]

//Add TableView Delegate and Data Source Methods

extension SelectCategoryViewController: UITableViewDelegate,UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return CategeryArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell")!
        cell.textLabel?.text = self.CategeryArray[indexPath.row]

        if self.CategeryArray[indexPath.row] == self.selectedCategoryValue {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        } 

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        self.selectedCategoryValue = CategeryArray[indexPath.row]
        self.tableView.reloadData()
    }
}

My View Like

enter image description here

NavigationController -> Mainview -> SelectedTableView

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41