How to send each tableViewcell data to collectionview in swift3 - ios

I have created dynamic forms, user enter number of forms, and i have created table cell for that. After submitting the all forms, all details should show in excel formate, for that i have created collection view but issue is that the only selected cell value is printing.
I use this for collectionView
https://github.com/brightec/CustomCollectionViewLayout
tableViewController.swift
struct Information {
var name: String
var phone: String
var email: String
var gender: String
init(name: String, phone: String, email: String, gender: String) {
self.name = name
self.phone = phone
self.gender = gender
self.email = email
}
import UIKit
class tableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet var tableView: UITableView!
#IBOutlet var nameText: UITextField!
var name: String = ""
var phone: String = ""
var email: String = ""
var gender: String = ""
var shop = Int()
var namevalue = String()
var phoneValue = String()
var emailValue = String()
var genderValue = String()
var formData: [Information] = []
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return shop
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let shop1 = [shop]
return "Section \(shop1[section]))"
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
if indexPath.section == 0{
if indexPath.row == 0{
print("THis is print 1",cell.emailText.text as Any)
}
}
name = cell.nameText.text!
phone = cell.phoneText.text!
email = cell.emailText.text!
gender = cell.genderText.text!
print("This is name",name)
let data = Information(name: name, phone: phone, email: email, gender: gender)
formData.append(data)
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
for _ in 0..<shop {
print("THis is print 3",shop)
}
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath!)! as! myTableViewCell
name = currentCell.nameText.text!
phone = currentCell.phoneText.text!
email = currentCell.emailText.text!
gender = currentCell.genderText.text!
print("Hello hello3",formData)
print("THis is print 4",currentCell.self.emailText.text as Any)
tableView.reloadData()
}
#IBAction func submitButton(_ sender: Any) {
tableView.reloadData()
print("THis is print 6",formData)
let mainController : CollectionViewController = CollectionViewController(nibName: "CollectionViewController", bundle: nil)
mainController.getShop = shop+1
mainController.formData = formData
// mainController.getPhone = phone
// mainController.getEmail = email
// mainController.getGender = gender
self.navigationController?.pushViewController(mainController, animated: true)}}
collectionViewController.swift
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var formData: [Information] = []
let dateCellIdentifier = "DateCellIdentifier"
let contentCellIdentifier = "ContentCellIdentifier"
#IBOutlet weak var collectionView: UICollectionView!
var getShop = Int()
var getName = String()
var getPhone = String()
var getEmail = String()
var getGender = String()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView .register(UINib(nibName: "DateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
self.collectionView .register(UINib(nibName: "ContentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
}
// MARK - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return getShop
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
for element in formData{
print("This is element",element.name)
}
if indexPath.section == 0 {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Number"
return dateCell
}
if indexPath.row == 1 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Name"
return dateCell
}
if indexPath.row == 2 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Phone"
return dateCell
}
if indexPath.row == 3 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Email"
return dateCell
}else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = "Gender"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
} else {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = String(indexPath.section)
if indexPath.section % 2 != 0 {
dateCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
dateCell.backgroundColor = UIColor.white
}
return dateCell
}
else if indexPath.row == 1
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getName
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 2
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getPhone
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 3
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getEmail
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getGender
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}}}}
1.I Entered number of form i want
2. no of forms displayed and i entered all different values for all different forms
3.But what happening is only one form details are printing

You shouldn't get your data in public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Because when you scroll up / down, this func will call again and again.
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let k = indexPath.row+1
let j:String? = String(k)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
cell.formNoLabel.text = j
for indexPath in 0..<shop{
print(String(indexPath))
}
if indexPath.section == 0{
if indexPath.row == 0{
print("THis is print 1",cell.emailText.text as Any)
}
}
var data : Information
if indexPath.row < formData.count {
data = formData[indexPath.row]
} else {
data = Information(name: name, phone: phone, email: email, gender: gender)
formData.append(data)
}
cell.data = data
print("THis is print 2",formData)
//cell.nameLabel.text = Array[indexPath.row]
// cell.nameText.text = Array[indexPath.row]
return cell
}
And in myTableViewCell class, you should change to
class myTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet var nameLabel: UILabel!
#IBOutlet var nameText: UITextField!
#IBOutlet var phoneText: UITextField!
#IBOutlet var emailText: UITextField!
#IBOutlet var genderText: UITextField!
#IBOutlet var formNoLabel: UILabel!
var data: Information! {
didSet {
nameText.text = data.name
phoneText.text = data.phone
emailText.text = data.email
genderText.text = data.gender
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
nameText.delegate = self
phoneText.delegate = self
emailText.delegate = self
genderText.delegate = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
print(currentCell.textLabel!.text!)
}
func textFieldDidEndEditing(_ textField: UITextField) {
let text = textField.text!
if textField === nameText {
data.name = text
} else if textField === phoneText {
data.phone = text
} else if textField === emailText {
data.email = text
} else if textField === genderText {
data.gender = text
}
}
}
Remember change struct Information to class Information

Related

How to increment and decrement value of label in tableview and make total price from label value in swift?

now cell value are dynamically and its look after calling api.
I want to make total of all tickets price at last. I refer this link How do I increment/decrement a label value with two buttons pressed in tableview Swift and make changes in my code but didn't work for me.
struct Product {
var price = 0
}
class TicketBookingVC: UIViewController , UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tblView: UITableView!
#IBOutlet weak var mainTblView: UIView!
var bookingDetails = NSDictionary()
var productArray = [Product]()
var product : Product!
private var counterValue = 1
var productIndex = 0
var counterLbl = UILabel()
#IBOutlet weak var bookBtn: UIButton!
#IBOutlet weak var eventImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
for _ in 0...10{
productArray.append(Product(price: 1))
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
else if section == 1{
return 4
}
else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellfirst", for: indexPath)
cell.selectionStyle = .none
return cell
}
else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellsecond", for: indexPath)
let mainViewCell = cell.contentView.viewWithTag(2000) as! UIView
let normalView = cell.contentView.viewWithTag(2001) as! UIView
let eventName = cell.contentView.viewWithTag(2003) as! UILabel
let eventPrice = cell.contentView.viewWithTag(2004) as! UILabel
counterLbl = cell.contentView.viewWithTag(2007) as! UILabel
let decrementBtn = cell.contentView.viewWithTag(2005) as! UIButton
let incrementBtn = cell.contentView.viewWithTag(2006) as! UIButton
decrementBtn.addTarget(self, action:#selector(self.decrementbuttonClicked), for: .touchUpInside)
incrementBtn.addTarget(self, action:#selector(self.incrementbuttonClicked), for: .touchUpInside)
product = productArray[indexPath.row]
counterLbl.text = "\(product.price)"
cell.selectionStyle = .none
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellthird", for: indexPath)
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0{
return UITableView.automaticDimension
}
else{
return 80
//return UITableView.automaticDimension
}
}
#objc func decrementbuttonClicked() {
print("Button decrement")
if(counterValue != 1){
counterValue -= 1;
}
self.counterLbl.text = "\(counterValue)"
product.price = counterValue
}
#objc func incrementbuttonClicked() {
counterValue += 1;
self.counterLbl.text = "\(counterValue)"
product.price = counterValue
}
func addProductToCart(product: Product, atindex: Int) {
productArray[atindex] = product
calculateTotal()
}
func calculateTotal()
{
var totalValue = 0
for objProduct in productArray {
totalValue += objProduct.price
}
self.eventPrice.text = "Total \(totalValue)"
}
}
when I increment or decrement value of first cell it reflect in 4th cell. please help. I am new at swift.
This is due to cell reuse. You should set a model for each cell

Showing the same content in the every cells while scrolling the cells

I have 5 types of tableview cells.And i ave one basecell.
BaseCell:-
import UIKit
class BaseCell: UITableViewCell {
// MARK: Internal Properties
var type: CellType!
var textChangedBlock: ((String) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setOptions(Options1:NH_OptionsModel){}
func setOptions(OptionsSelected:NH_OptionsModel){}
func setOptions1(OptionsSelected:NH_OptionsModel){}
func setOptions(OptionsisSelected:NH_OptionsModel){}
func setOptions1(OptionsisSelected:NH_OptionsModel){}
}
One Tableviewcell:-
class RadioTypeCell: BaseCell {
var actionBlock: (() -> Void)?
#IBOutlet weak var optionLabel: UILabel?
#IBOutlet weak var buttonType: UIButton?
override func setOptions(Options1:NH_OptionsModel) {
self.optionLabel?.text = Options1.values
}
override func setOptions1(OptionsSelected:NH_OptionsModel) {
self.buttonType?.isHidden = false
buttonType?.setBackgroundImage(UIImage(named: "radio_check.png"), for: .normal)
print(OptionsSelected.isSelected)
OptionsSelected.isSelected = true
print(OptionsSelected.isSelected)
}
override func setOptions(OptionsisSelected:NH_OptionsModel) {
self.buttonType?.isHidden = false
buttonType?.setBackgroundImage(UIImage(named: "radio_uncheck.png"), for: .normal)
print(OptionsisSelected.isSelected)
OptionsisSelected.isSelected = false
print(OptionsisSelected.isSelected)
}
#IBAction func onActionButton(btn: UIButton) {
print("click the cell")
actionBlock?()
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func prepareForReuse() {
super.prepareForReuse()
}
}
In viewcontroller:-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType)
print(model.answerType?.rawValue)
let c = model.answerType
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)
let optionModel = questionViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)
cell.type = c?.cellType()
currentCell(c: cell, index: indexPath.row)
return cell
}
So In the tableview cell ,it contain
the following things.
1.One button
2.Label(Here showing the options)
So while selecting the one option i will click on the button,at that time the button has set the background image.Initially the image will be radio button with uncheck after while clicking the button the button image becomes radio button check.
And i given the code in the didselect the cell from the tableview.
Now my problem is after selecting the cells and then scrolling the tableview .The cells content is not refreshing .How to solve the problem.
And my tableview has sections.
My model:-
import UIKit
typealias JSONDictionary = [String:Any]
enum AnswerType:Int
{
case CheckBoxCell = 1
case RadioTypeCell
case SmileyTypeCell
case StarRatingTypeCell
case TextTypeCell
func cellType() -> CellType{
switch self {
case .CheckBoxCell: return .checkboxtype
case .RadioTypeCell :return .radiotype
case .SmileyTypeCell: return .smileytype
case .StarRatingTypeCell: return .starratingtype
case .TextTypeCell: return .textfieldtype
default: return .radiotype
}
}
}
class QuestionListModel: NSObject {
var dataListArray33:[NH_OptionsModel] = []
var id:Int!
var question:String!
var buttontype:String!
var options:[String]?
var v:String?
var answerType:NHAnswerType?
var optionsModelArray:[OptionsModel] = []
init(dictionary :JSONDictionary) {
guard let question = dictionary["question"] as? String,
let typebutton = dictionary["button_type"] as? String,
let id = dictionary["id"] as? Int
else {
return
}
self.answerType = AnswerType(rawValue: Int(typebutton)!)
if let options = dictionary["options"] as? [String]{
print(options)
print(options)
for values in options{
print(values)
let optionmodel = OptionsModel(values: values)
self.optionsModelArray.append(optionmodel)
}
}
self.buttontype = typebutton
self.question = question
self.id = id
}
}
my viewmodel:-
var OptionListArray:Array<OptionsModel>? = []
init(withdatasource newDatasourceModel:QuestionDataSourceModel,withmodel model:HomeModel) {
datasourceModel = newDatasourceModel
homemodel1 = model
print(homemodel1)
print(datasourceModel.dataListArray)
}
func numberOfSections() -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}
func titleForHeaderInSection(atsection section: Int) -> QuestionListModel {
return datasourceModel.dataListArray![section]
}
func numberOfRowsIn(section:Int) -> Int {
print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0
// return self.questionsModelArray?[section].optionsModelArray.count ?? 0
}
func datafordisplay(atindex indexPath: IndexPath) -> NH_OptionsModel{
print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])
return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}
didselect tableview:-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if questionViewModel.isselected == true{
let selectedIndexPathAtCurrentSection = questionViewModel.selectedIndexPaths.filter({ $0.section == indexPath.section})
for indexPath in selectedIndexPathAtCurrentSection {
tableview.deselectRow(at: indexPath, animated: true)
if let indexOf = questionViewModel.selectedIndexPaths.index(of: indexPath) {
questionViewModel.selectedIndexPaths.remove(at: indexOf)
let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
questionViewModel.question = questionModel.question
questionViewModel.insertQuestList(answer: questionViewModel.question!, Index: indexPath.section)
let c = questionModel.answerType
print(c)
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableview.cellForRow(at: indexPath) as? BaseCell
print(cell)
// let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell
questionViewModel.button = questionModel.buttontype
let value: Int = questionModel.id
let string = String(describing: value)
questionViewModel.question_id = string
questionViewModel.question = questionModel.question
let model = questionViewModel.datafordisplay(atindex: indexPath)
if questionModel.buttontype == "2" {
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions(OptionsisSelected:questionViewModel.datafordisplay(atindex: indexPath))
}
else if questionModel.buttontype == "1"{
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions1(OptionsisSelected:questionViewModel.datafordisplay(atindex: indexPath))
}
questionViewModel.isselected = model.isSelected!
}
}
questionViewModel.selectedIndexPaths.append(indexPath)
let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
let c = questionModel.answerType
print(c)
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableview.cellForRow(at: indexPath) as? BaseCell
print(cell)
let model = questionViewModel.datafordisplay(atindex: indexPath)
if questionModel.buttontype == "1"{
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
}
else if questionModel.buttontype == "2"{
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions1(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
// }
print(questionViewModel.answers1)
}
questionViewModel.isselected = model.isSelected!
questionViewModel.question = questionModel.question
questionViewModel.questionlist(answer: questionViewModel.question!)
questionViewModel.insertQuestList(answer: questionModel.question, Index: indexPath.section)
let value: Int = questionModel.id
let string = String(describing: value)
questionViewModel.question_id = string
questionViewModel.question(answer: questionViewModel.question_id!)
questionViewModel.questIdList(answer: questionViewModel.question_id!, Index: indexPath.section)
questionViewModel.button = questionModel.buttontype
}
// False condition
else if questionViewModel.isselected == false{
let questionModel = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
questionViewModel.question = questionModel.question
questionViewModel.insertQuestList(answer: questionViewModel.question!, Index: indexPath.section)
let c = questionModel.answerType
print(c)
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableview.cellForRow(at: indexPath) as? BaseCell
print(cell)
let model = questionViewModel.datafordisplay(atindex: indexPath)
print(model.values)
if questionModel.buttontype == "1"{
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
print(questionViewModel.answers1)
}
//Radio Button
else if questionModel.buttontype == "2"{
questionViewModel.insertStrng(answer: model.values!, Index: indexPath.section)
cell?.setOptions1(OptionsSelected:questionViewModel.datafordisplay(atindex: indexPath))
// }
print(questionViewModel.answers1)
}
questionViewModel.isselected = model.isSelected!
let value: Int = questionModel.id
let string = String(describing: value)
questionViewModel.question_id = string
questionViewModel.question(answer: questionViewModel.question_id!)
questionViewModel.questIdList(answer: questionViewModel.question_id!, Index: indexPath.section)
questionViewModel.button = questionModel.buttontype
questionViewModel.selectedIndexPaths.append(indexPath)
}
}
Make sure your model object value is update on button clicked event and you need to update cell ui in Cell at index.
Here is reference code for how to update ui at cell at index.
Note: Code is not checked in xcode so may be some minor error.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType)
print(model.answerType?.rawValue)
let c = model.answerType
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)
let optionModel = questionViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)
if optionModel.isSelected {
cell.setOptions1(OptionsisSelected:optionModel)
} else {
cell.setOptions(OptionsisSelected:optionModel)
}
cell.type = c?.cellType()
currentCell(c: cell, index: indexPath.row)
return cell
}

Send Form data to CollectionView

I have created dynamic forms, user enter number of forms, and i have created table cell for that. After submitting the all forms, all details should show in excel formate, for that i have created collection view but issue is that the only selected cell value is printing.
Please See this is github Code that i have used in my project
https://github.com/brightec/CustomCollectionViewLayout
tableViewController.swift
struct Information {
var name: String
var phone: String
var email: String
var gender: String
init(name: String, phone: String, email: String, gender: String) {
self.name = name
self.phone = phone
self.gender = gender
self.email = email
}
import UIKit
class tableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet var tableView: UITableView!
#IBOutlet var nameText: UITextField!
var name: String = ""
var phone: String = ""
var email: String = ""
var gender: String = ""
var shop = Int()
var namevalue = String()
var phoneValue = String()
var emailValue = String()
var genderValue = String()
var formData: [Information] = []
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return shop
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let shop1 = [shop]
return "Section \(shop1[section]))"
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
if indexPath.section == 0{
if indexPath.row == 0{
print("THis is print 1",cell.emailText.text as Any)
}
}
name = cell.nameText.text!
phone = cell.phoneText.text!
email = cell.emailText.text!
gender = cell.genderText.text!
print("This is name",name)
let data = Information(name: name, phone: phone, email: email, gender: gender)
formData.append(data)
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
for _ in 0..<shop {
print("THis is print 3",shop)
}
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath!)! as! myTableViewCell
name = currentCell.nameText.text!
phone = currentCell.phoneText.text!
email = currentCell.emailText.text!
gender = currentCell.genderText.text!
print("Hello hello3",formData)
print("THis is print 4",currentCell.self.emailText.text as Any)
tableView.reloadData()
}
#IBAction func submitButton(_ sender: Any) {
tableView.reloadData()
print("THis is print 6",formData)
let mainController : CollectionViewController = CollectionViewController(nibName: "CollectionViewController", bundle: nil)
mainController.getShop = shop+1
mainController.formData = formData
// mainController.getPhone = phone
// mainController.getEmail = email
// mainController.getGender = gender
self.navigationController?.pushViewController(mainController, animated: true)}}
CollectionViewController.swift
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var formData: [Information] = []
let dateCellIdentifier = "DateCellIdentifier"
let contentCellIdentifier = "ContentCellIdentifier"
#IBOutlet weak var collectionView: UICollectionView!
var getShop = Int()
var getName = String()
var getPhone = String()
var getEmail = String()
var getGender = String()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView .register(UINib(nibName: "DateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
self.collectionView .register(UINib(nibName: "ContentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
}
// MARK - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return getShop
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
for element in formData{
print("This is element",element.name)
}
if indexPath.section == 0 {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Number"
return dateCell
}
if indexPath.row == 1 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Name"
return dateCell
}
if indexPath.row == 2 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Phone"
return dateCell
}
if indexPath.row == 3 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Email"
return dateCell
}else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = "Gender"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
} else {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = String(indexPath.section)
if indexPath.section % 2 != 0 {
dateCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
dateCell.backgroundColor = UIColor.white
}
return dateCell
}
else if indexPath.row == 1
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getName
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 2
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getPhone
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 3
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getEmail
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getGender
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}}}}

How to add Value from tableViewCell to CollectionView

I have created dynamic forms, user enter number of forms, and i have created table cell for that. After submitting the all forms, all details should show in excel formate, for that i have created collection view but issue is that the only selected cell value is printing.
https://github.com/brightec/CustomCollectionViewLayout
tableViewController.swift
struct Information {
var name: String
var phone: String
var email: String
var gender: String
init(name: String, phone: String, email: String, gender: String) {
self.name = name
self.phone = phone
self.gender = gender
self.email = email
}
import UIKit
class tableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet var tableView: UITableView!
#IBOutlet var nameText: UITextField!
var name: String = ""
var phone: String = ""
var email: String = ""
var gender: String = ""
var shop = Int()
var namevalue = String()
var phoneValue = String()
var emailValue = String()
var genderValue = String()
var formData: [Information] = []
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return shop
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let shop1 = [shop]
return "Section \(shop1[section]))"
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
if indexPath.section == 0{
if indexPath.row == 0{
print("THis is print 1",cell.emailText.text as Any)
}
}
name = cell.nameText.text!
phone = cell.phoneText.text!
email = cell.emailText.text!
gender = cell.genderText.text!
print("This is name",name)
let data = Information(name: name, phone: phone, email: email, gender: gender)
formData.append(data)
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
for _ in 0..<shop {
print("THis is print 3",shop)
}
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath!)! as! myTableViewCell
name = currentCell.nameText.text!
phone = currentCell.phoneText.text!
email = currentCell.emailText.text!
gender = currentCell.genderText.text!
print("Hello hello3",formData)
print("THis is print 4",currentCell.self.emailText.text as Any)
tableView.reloadData()
}
#IBAction func submitButton(_ sender: Any) {
tableView.reloadData()
print("THis is print 6",formData)
let mainController : CollectionViewController = CollectionViewController(nibName: "CollectionViewController", bundle: nil)
mainController.getShop = shop+1
mainController.formData = formData
// mainController.getPhone = phone
// mainController.getEmail = email
// mainController.getGender = gender
self.navigationController?.pushViewController(mainController, animated: true)}}
CollectionViewController.swift
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var formData: [Information] = []
let dateCellIdentifier = "DateCellIdentifier"
let contentCellIdentifier = "ContentCellIdentifier"
#IBOutlet weak var collectionView: UICollectionView!
var getShop = Int()
var getName = String()
var getPhone = String()
var getEmail = String()
var getGender = String()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView .register(UINib(nibName: "DateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
self.collectionView .register(UINib(nibName: "ContentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
}
// MARK - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return getShop
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
for element in formData{
print("This is element",element.name)
}
if indexPath.section == 0 {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Number"
return dateCell
}
if indexPath.row == 1 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Name"
return dateCell
}
if indexPath.row == 2 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Phone"
return dateCell
}
if indexPath.row == 3 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Email"
return dateCell
}else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = "Gender"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
} else {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = String(indexPath.section)
if indexPath.section % 2 != 0 {
dateCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
dateCell.backgroundColor = UIColor.white
}
return dateCell
}
else if indexPath.row == 1
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getName
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 2
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getPhone
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 3
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getEmail
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getGender
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}}}}
1.I Entered number of form i want
2. no of forms displayed and i entered all different values for all different forms
3.But what happening is only one form details are printing

How to pass data from all TableViewCell to CollectionView in Swift3

I have created dynamic forms, user enter number of forms, and i have created table cell for that. After submitting the all forms, all details should show in excel formate, for that i have created collection view but issue is that the only selected cell value is printing.
class tableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
#IBOutlet var nameText: UITextField!
#IBOutlet var tableView: UITableView!
var shop = Int()
var namevalue = [String]()
var phoneValue = String()
var emailValue = String()
var genderValue = String()
var isSelectAll = false;
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return shop
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let k = indexPath.row+1
let j:String? = String(k)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
if(isSelectAll==true)
{
cell.backgroundColor = UIColor.blue;
}
else
{
cell.backgroundColor = UIColor.white;
}
cell.formNoLabel.text = j
for _ in 0..<shop{
print(cell.nameLabel.text as Any)
}
if indexPath.section == 0{
if indexPath.row == 0{
print(cell.emailText.text as Any)
}
}
for shop in 0..<(indexPath.count){
let thisPath = indexPath[shop]
print(thisPath as Any)
}
return cell
} public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
for _ in 0..<shop {
let currentCell = tableView.cellForRow(at: indexPath!)! as! myTableViewCell
print(currentCell.self.nameText.text as Any)
}
let currentCell = tableView.cellForRow(at: indexPath!)! as! myTableViewCell
print(currentCell.self.emailText.text as Any)
namevalue.append(currentCell.nameText.text!)
// namevalue = [currentCell.nameText.text!]
phoneValue = currentCell.phoneText.text!
emailValue = currentCell.emailText.text!
genderValue = currentCell.genderText.text!
#IBAction func submitButton(_ sender: Any) {
let mainController : CollectionViewController = CollectionViewController(nibName: "CollectionViewController", bundle: nil)
mainController.getShop = shop+1
mainController.getName = namevalue
mainController.getPhone = phoneValue
mainController.getEmail = emailValue
mainController.getGender = genderValue
self.navigationController?.pushViewController(mainController, animated: true)
// self.present(mainController, animated: true, completion: nil)
}
#IBAction func Select(_ sender: Any) {
isSelectAll = true;//on button action
print(namevalue)
self.tableView.reloadData()//on button action
}
}
class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate**
let dateCellIdentifier = "DateCellIdentifier"
let contentCellIdentifier = "ContentCellIdentifier"
#IBOutlet weak var collectionView: UICollectionView!
var getShop = Int()
var getName = String()
var getPhone = String()
var getEmail = String()
var getGender = String()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView .register(UINib(nibName: "DateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
self.collectionView .register(UINib(nibName: "ContentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
}
// MARK - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return getShop
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Number"
return dateCell
}
if indexPath.row == 1 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Name"
return dateCell
}
if indexPath.row == 2 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Phone"
return dateCell
}
if indexPath.row == 3 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.backgroundColor = UIColor.white
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = "Email"
return dateCell
}else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = "Gender"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
} else {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: dateCellIdentifier, for: indexPath) as! DateCollectionViewCell
dateCell.dateLabel.font = UIFont.systemFont(ofSize: 13)
dateCell.dateLabel.textColor = UIColor.black
dateCell.dateLabel.text = String(indexPath.section)
if indexPath.section % 2 != 0 {
dateCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
dateCell.backgroundColor = UIColor.white
}
return dateCell
}
if indexPath.row == 1
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getName
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 2
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getPhone
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else if indexPath.row == 3
{
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getEmail
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCell(withReuseIdentifier: contentCellIdentifier, for: indexPath) as! ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFont(ofSize: 13)
contentCell.contentLabel.textColor = UIColor.black
contentCell.contentLabel.text = getGender
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.white
}
return contentCell
}
}
}
I have an idea for your problem.
Step 1: Create new struct to save information.
struct Information {
var name: String
var phone: String
var email: String
var gender: String
init(name: String, phone: String, email: String, gender: String) {
self.name = name
self.phone = phone
self.gender = gender
self.email = email
}
}
Step 2:
In class tableViewController, create new variable:
var formData: [Information] = []
Step 3: Each time user enter one form at cell in tableView:
let name = cell.nameText.text!
let phone = cell.phoneText.text!
let email = cell.emailText.text!
let gender = cell.genderText.text!
data = Information(name: name, phone: phone, email: email, gender: gender)
formData.append(data)
Step 4: In class CollectionViewController, declare new variable:
var formData: [Information] = []
Step 5: Create a header for collectionView:
| Name | Phone | Email | Gender |
Step 6: Each cell in collectionView:
| formData[indexPath].name | formData[indexPath].name | formData[indexPath].name | formData[indexPath].name |
Step 7:
#IBAction func submitButton(_ sender: Any) {
mainController.formData = formData.
// do somethings
...
}
I don't know why you need a lot submit buttons to create many controllers, but let try my idea.

Resources