简体   繁体   English

如何将每个tableViewcell数据发送到swift3中的collectionview

[英]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. 提交所有表格后,所有细节都应显示在excel formate中,因为我已经创建了集合视图,但问题是唯一选定的单元格值是打印。 I use this for collectionView 我将它用于collectionView

https://github.com/brightec/CustomCollectionViewLayout https://github.com/brightec/CustomCollectionViewLayout

tableViewController.swift 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 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 1.I输入我想要的表格数量

在此输入图像描述

2. no of forms displayed and i entered all different values for all different forms 2.没有显示的表格,我为所有不同的表格输入了所有不同的值

在此输入图像描述

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 你不应该在public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell获取你的数据

Because when you scroll up / down, this func will call again and again. 因为当你向上/向下滚动时,这个func会一次又一次地调用。

 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 myTableViewCell类中,您应该更改为

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 请记住将struct Information更改为class Information

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Swift3中将数据从所有TableViewCell传递到CollectionView - How to pass data from all TableViewCell to CollectionView in Swift3 如何在Swift3中将collectionView数据发送到另一个ViewController? - How to send collectionView data to another ViewController in swift3? 如何使用swift3在TableViewCell中的CollectionViewCell中动态显示Server中的数据? - How to display dynamically data from Server in CollectionViewCell in TableViewCell with swift3? TableViewCell滚动中的CollectionView在swift3中不流畅 - CollectionView in TableViewCell scroll isn't smooth in swift3 如何将Item插入到collectionView中? 迅捷3 - How to insert Item to a collectionView ? Swift3 Swift:如何以编程方式在 TableViewCell 中显示 CollectionView - Swift: How to display a CollectionView inside a TableViewCell Programmatically swift3中tableviewcell中图像的手势识别器 - Gesturerecogniser for image in tableviewcell in swift3 如何切换UIStackView中的布置的子视图的可见性,多数民众赞成在tableViewCell swift3 - how to to toggle visibility of arrangedSubviews in a UIStackView, thats inside a tableViewCell swift3 tableViewCell数据显示中的CollectionView - CollectionView in tableViewCell data display iOS:在Swift3的collectionView中,当我向下滚动时,collectionView会自动位于顶部。 如何停止呢? - iOS: in Swift3, in my collectionView, when I scroll downstair, the collectionView comes automatically at the top. How to stop it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM