"Expected Declaration" with "{" - ios

Such a beginner question but I am getting this error and I have tried to rearrange the braces in a million different ways but still return this error one way or another. I have managed to arrange the braces in a fashion to return only one error in one place. Here is a picture of the location of the error as well as the code.
//
// ViewController.swift
// TwoCVs
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class FirstCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var buttonOne: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonOne != nil else { return }
buttonOne.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonOne.layer.cornerRadius = 10
buttonOne.clipsToBounds = true
buttonOne.layer.borderWidth = 1.0
buttonOne.layer.borderColor = UIColor.white.cgColor
}
}
class SecondCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var buttonTwo: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonTwo != nil else { return }
buttonTwo.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonTwo.layer.cornerRadius = 10
buttonTwo.clipsToBounds = true
buttonTwo.layer.borderWidth = 1.0
buttonTwo.layer.borderColor = UIColor.white.cgColor
}
}
class ThirdCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var buttonThree: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonThree != nil else { return }
buttonThree.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonThree.layer.cornerRadius = 10
buttonThree.clipsToBounds = true
buttonThree.layer.borderWidth = 1.0
buttonThree.layer.borderColor = UIColor.white.cgColor
}
}
class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var firstCV: UICollectionView!
#IBOutlet weak var secondCV: UICollectionView!
#IBOutlet weak var thirdCV: UICollectionView!
#IBAction func Buttons(_ sender: Any) {
if let btn = sender as? UIButton {
print(btn.restorationIdentifier!)
guard let button = sender as? UIButton else { return }
guard let id = Int(button.restorationIdentifier!) else {return}
if id == 0 {
performSegue(withIdentifier: "Good Work", sender: btn)
}
else if id == 1 {
performSegue(withIdentifier: "Nice Try", sender: btn)
}
else if id == 6 {
performSegue(withIdentifier: "Second 1", sender: btn)
}
}
}
let firstData: [String] = [
"Good Work", "Nice Try", "Btn 3", "Btn 4", "Btn 5", "Btn 6"
]
let secondData: [String] = [
"Second 1", "Second 2", "Second 3", "Second 4", "Second 5", "Second 6", "Second 7"
]
let thirdData: [String] = [
"Action 1", "Action 2", "Action 3", "Action 4", "Action 5", "Action 6"]
override func viewDidLoad() {
super.viewDidLoad()
firstCV.dataSource = self
firstCV.delegate = self
secondCV.dataSource = self
secondCV.delegate = self
thirdCV.dataSource = self
thirdCV.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// if it's the First Collection View
if collectionView == firstCV {
return firstData.count
// it's not the First Collection View, so it's the Second one
return secondData.count
}
return thirdData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == firstCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
cell.buttonOne.setTitle(firstData[indexPath.item], for: []) //allows for button title change in code above
cell.buttonOne.restorationIdentifier = "\(indexPath.row)"
return cell
}
// it's not the First Collection View, so it's the Second one
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
cell.buttonTwo.restorationIdentifier = "\(indexPath.row + firstData.count)"
return cell
}
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath) as! ThirdCollectionViewCell
cell.buttonThree.setTitle(thirdData[indexPath.item], for: [])
cell.buttonThree.restorationIdentifier = "\(indexPath.row + secondData.count)"
return cell
}
}

I guess maybe you want your last method to be like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == firstCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
cell.buttonOne.setTitle(firstData[indexPath.item], for: []) //allows for button title change in code above
cell.buttonOne.restorationIdentifier = "\(indexPath.row)"
return cell
} else if collectionView == secondCV {
// it's not the First Collection View, so it's the Second one
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
cell.buttonTwo.restorationIdentifier = "\(indexPath.row + firstData.count)"
return cell
} else if collectionView == thirdCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath) as! ThirdCollectionViewCell
cell.buttonThree.setTitle(thirdData[indexPath.item], for: [])
cell.buttonThree.restorationIdentifier = "\(indexPath.row + secondData.count)"
return cell
}
}

Related

Segue for UIButton Opens Same Page Regardless of which Button is Pressed

Below is my app in the simulator. It has two CollectionViews each with multiple UIButtons. I currently have a segue for UIButtons: "Good Work" and "Nice Try" in the 'firstData' string you see below, in the code.
Regardless of which one I press, however, it opens to the same view linked to identifer "Good Work." I am at a loss of how to get this to open to their own pages.
Code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class FirstCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var buttonOne: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonOne != nil else { return }
buttonOne.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonOne.layer.cornerRadius = 10
buttonOne.clipsToBounds = true
buttonOne.layer.borderWidth = 1.0
buttonOne.layer.borderColor = UIColor.white.cgColor
}
}
class SecondCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var buttonTwo: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonTwo != nil else { return }
buttonTwo.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonTwo.layer.cornerRadius = 10
buttonTwo.clipsToBounds = true
buttonTwo.layer.borderWidth = 1.0
buttonTwo.layer.borderColor = UIColor.white.cgColor
}
}
class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var firstCV: UICollectionView!
#IBOutlet weak var secondCV: UICollectionView!
#IBAction func Buttons(_ sender: Any) {
if let btn = sender as? UIButton {
print(btn.restorationIdentifier!)
guard let button = sender as? UIButton else { return }
guard let id = Int(button.restorationIdentifier!) else {return}
if id < firstData.count {
performSegue(withIdentifier: "Good Work", sender: btn)
}
else {
performSegue(withIdentifier: "Nice Try", sender: btn)
}
print(button.restorationIdentifier!)
}
}
let firstData: [String] = [
"Good Work", "Nice Try", "Btn 3", "Btn 4", "Btn 5", "Btn 6"
]
let secondData: [String] = [
"Second 1", "Second 2", "Second 3", "Second 4", "Second 5", "Second 6"
]
override func viewDidLoad() {
super.viewDidLoad()
firstCV.dataSource = self
firstCV.delegate = self
secondCV.dataSource = self
secondCV.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// if it's the First Collection View
if collectionView == firstCV {
return firstData.count
}
// it's not the First Collection View, so it's the Second one
return secondData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == firstCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
cell.buttonOne.setTitle(firstData[indexPath.item], for: []) //allows for button title change in code above
cell.buttonOne.restorationIdentifier = "\(indexPath.row)"
return cell
}
// it's not the First Collection View, so it's the Second one
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
cell.buttonTwo.restorationIdentifier = "\(indexPath.row + firstData.count)"
return cell
}
}
The reason you only get to "Good Work" ...
In cellForItemAt you are doing this:
cell.buttonOne.restorationIdentifier = "\(indexPath.row)"
That sets the .restorationIdentifier for "Good Work" button to 0 (Zero) and the .restorationIdentifier for "Nice Try" button to 1.
Then, in #IBAction func Buttons(_ sender: Any) {:
if id < firstData.count {
performSegue(withIdentifier: "Good Work", sender: btn)
}
else {
performSegue(withIdentifier: "Nice Try", sender: btn)
}
Since firstData.count equals 6, and the id from the buttons will be either 0 or 1, the are, of course, each less-than-6.
Using your approach, you would likely want to do this:
if id == 0 {
performSegue(withIdentifier: "Good Work", sender: btn)
}
else if id == 1 {
performSegue(withIdentifier: "Nice Try", sender: btn)
}

Align the collectionviewcells center

I current have a collectionview attached to a array of strings. I want to align these cells center regardless of the number of things in the array. I am not sure how to do it, and where to do it.
import UIKit
class MyButtonCell: UICollectionViewCell{
#IBOutlet weak var buttonOne: UIButton!
#IBOutlet weak var targetButton: UIButton!
var callback: (() -> ())?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.layer.borderWidth = 1
contentView.layer.borderColor = UIColor.black.cgColor
}
#IBAction func buttonTapped(_ sender: UIButton) {
callback?()
}
}
class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let buttonTitles: [String] = [
"4", "6", "7", "8"
]
var targetButtonTitles: [String] = [
"", "", "", ""
]
var current:String = ""
#IBOutlet var collectionView: UICollectionView!
#IBOutlet var targetCollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
targetCollection.delegate = self
targetCollection.dataSource = self
collectionView.delegate = self
collectionView.dataSource = self
collectionView.tag = 1
targetCollection.tag = 2
}
func centerItemsInCollectionView(cellWidth: Double, numberOfItems: Double, spaceBetweenCell: Double, collectionView: UICollectionView) -> UIEdgeInsets {
let totalWidth = cellWidth * numberOfItems
let totalSpacingWidth = spaceBetweenCell * (numberOfItems - 1)
let leftInset = (collectionView.frame.width - CGFloat(totalWidth + totalSpacingWidth)) / 2
let rightInset = leftInset
return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 1 {
return buttonTitles.count
} else {
return targetButtonTitles.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// set the button title (and any other properties)
if collectionView.tag == 1 {
// Setup here your cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
cell.callback = {
print("Button was tapped at \(indexPath)")
self.targetButtonTitles[indexPath.item] = self.buttonTitles[indexPath.item]
//print(self.targetButtonTitles)
self.current = self.buttonTitles[indexPath.item]
print(self.current)
//collectionView.reloadData()
// do what you want when the button is tapped
}
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
return cell
} else {
// Setup here your targetCell
let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
targetCell.callback = {
if self.current != ""{
self.targetButtonTitles[indexPath.item] = self.current
print(self.targetButtonTitles)
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
self.current = ""
}else{
self.targetButtonTitles[indexPath.item] = ""
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
}
}
return targetCell
}
}
}
As you can see, right now they are just starting from the left. So, How should I align them in the middle.

Checkout shopping cart UICollectionViewCell

I have a shopping cart check out flow using a UICollectionView with full page UICollectionViewCells. When the add button is pressed, the remove button is then visible and vice versa. For some reason when add and remove are repeatedly pressed it disrupts the other cells. It will show remove button on another cell when the add button on that cell was never even pressed. I am not sure what's wrong with my logic.
protocol PostCellDelegate {
func removeButtonTapped(cell: PostCell)
func addTapped(cell: PostCell)
}
class PostCell: UICollectionViewCell {
var currentPrice: Float = 0
var delegate: PostCellDelegate?
func set(name: String, brand: String, price: String, image: String){
nameLabel.text = name
brandLabel.text = brand
priceLabel.text = "$\(price)"
photoImageView.loadImage(urlString: image)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.myButton.addTarget(self, action: #selector(addButtonTapped(sender:)), for: .touchUpInside)
self.removeButton.addTarget(self, action: #selector(subButtonTapped(sender:)), for: .touchUpInside)
self.contentView.addSubview(containerView)
setupCellConstraints()
}
#objc func addButtonTapped(sender: UIButton){
self.delegate?.addTapped(cell: self)
sender.isHidden = true
}
#objc func subButtonTapped(sender: UIButton){
self.delegate?.removeButtonTapped(cell: self)
sender.isHidden = true
}
}
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {
var totalPrice = Float()
private var hiddenRows = Set<Int>()
var finalList = [Item]()
#objc func addTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell) else {return}
hiddenRows.insert(indexPath.row)
cell.removeButton.isHidden = false
let item = itemsArr[indexPath.row]
finalList.append(item)
collectionView?.reloadData()
totalPrice += Float(item.price) ?? 0
}
#objc func removeButtonTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell) else {return}
hiddenRows.insert(indexPath.row)
cell.myButton.isHidden = false
let item = itemsArr[indexPath.row]
finalList.removeAll{ $0.name == item.name}
totalPrice -= Float(item.price) ?? 0
}
extension CollectionViewController {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PostCell
cell.delegate = self
let item = itemsArr[indexPath.row]
let page = itemsArr[indexPath.item]
cell.set(name: item.name, brand: item.brand, price: item.price, image: item.image_url)
if hiddenRows.contains(indexPath.row) {
cell.myButton.isHidden = true
cell.removeButton.isHidden = false
}else{
cell.removeButton.isHidden = true
cell.myButton.isHidden = false
}
return cell
}
Cells are reused you need to restore to default here in cellForItemAt
// restore default look here ( hide / show what you need )
if hiddenRows.contains(indexPath.row) {
cell.myButton.isHidden = true
cell.removeButton.isHidden = false
}else{
cell.removeButton.isHidden = true
cell.myButton.isHidden = false
}
It was because in removeButtonTapped I had hiddenRows.insert(indexPath.row) instead of hiddenRows.remove(indexPath.row). I don't know how I missed that.

Detect last cell in full page UICell UICollectionView

I have a checkout flow using a UICollectionViewController and a full page UICell. After the last cell(the last item) is displayed I want to add a summary page of all the items that have been added to the shopping cart along with a total price. I tried to check if the last item = itemsArray.count - 1 however it keeps printing "last item" in the cell before last. Also in the last cell, it prints "not last".
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {
var totalPrice = Float()
private var hiddenRows = Set<Int>()
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
cell.finalLabel.text = String(totalPrice)
cell.delegate = self
let item = itemsArr[indexPath.row]
cell.set(name: item.name, brand: item.brand, price: item.price)
if indexPath.row == itemsArr.count - 1 {
print("last item")
}else {
print("not last")
}
if hiddenRows.contains(indexPath.row) {
cell.myButton.isHidden = true
cell.removeButton.isHidden = false
}else{
cell.removeButton.isHidden = true
cell.myButton.isHidden = false
}
cell.finalLabel.text = String(totalPrice)
return cell
}
#objc func addTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell) else {return}
hiddenRows.insert(indexPath.row)
cell.removeButton.isHidden = false
let item = itemsArr[indexPath.row]
print(item.price)
totalPrice += Float(item.price) ?? 0
cell.finalLabel.text = String(totalPrice)
}
#objc func removeButtonTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell) else {return}
hiddenRows.insert(indexPath.row)
cell.myButton.isHidden = false
let item = itemsArr[indexPath.row]
totalPrice -= Float(item.price) ?? 0
cell.finalLabel.text = String(totalPrice)
}
}
protocol PostCellDelegate {
func removeButtonTapped(cell: PostCell)
func addTapped(cell: PostCell)
func didPressButton(_ tag: Int)
}
class PostCell: UICollectionViewCell {
var delegate: PostCellDelegate?
func set(name: String, brand: String, price: String){
nameLabel.text = name
brandLabel.text = brand
priceLabel.text = price
}
override init(frame: CGRect) {
super.init(frame: frame)
self.myButton.addTarget(self, action: #selector(addButtonTapped(sender:)), for: .touchUpInside)
self.removeButton.addTarget(self, action: #selector(subButtonTapped(sender:)), for: .touchUpInside)
setupCellConstraints()
}
#objc func buttonPressed(_ sender: UIButton) {
delegate?.didPressButton(sender.tag)
}
#objc func addButtonTapped(sender: UIButton){
self.delegate?.addTapped(cell: self)
sender.isHidden = true
}
#objc func subButtonTapped(sender: UIButton){
self.delegate?.removeButtonTapped(cell: self)
sender.isHidden = true
}
}

How to send each tableViewcell data 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.
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

Resources