菜鸟教程小白 发表于 2022-12-11 19:10:32

ios - 如何在 Swift 3 中将数据从所有 TableView Cell 传递到 CollectionView


                                            <p><p>我创建了动态表单,用户输入了表单数量,并且为此创建了表格单元格。提交所有表单后,所有详细信息都应以 excel 格式显示,因为我已经创建了 Collection View ,但问题是唯一选定的单元格值正在打印。</p>

<pre><code>class tableViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

    @IBOutlet var nameText: UITextField!
    @IBOutlet var tableView: UITableView!

    var shop = Int()
    var namevalue = ()
    var phoneValue = String()
    var emailValue = String()
    var genderValue = String()
    var isSelectAll = false;
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int

    {
      return shop
    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell

    {

      let k = indexPath.row+1
      let j:String? = String(k)

      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;, for: indexPath) as! myTableViewCell

      if(isSelectAll==true)
      {
            cell.backgroundColor = UIColor.blue;
      }
      else
      {
            cell.backgroundColor = UIColor.white;
      }
      cell.formNoLabel.text = j

      for _ in 0..&lt;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..&lt;(indexPath.count){

            let thisPath = indexPath
            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..&lt;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 =
      phoneValue = currentCell.phoneText.text!
      emailValue =currentCell.emailText.text!
      genderValue =currentCell.genderText.text!


         @IBAction func submitButton(_ sender: Any) {
          let mainController : CollectionViewController = CollectionViewController(nibName: &#34;CollectionViewController&#34;, 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
    }
}
</code></pre>

<hr/>

<pre><code>class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate**

    let dateCellIdentifier = &#34;DateCellIdentifier&#34;
    let contentCellIdentifier = &#34;ContentCellIdentifier&#34;
    @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: &#34;DateCollectionViewCell&#34;, bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
      self.collectionView .register(UINib(nibName: &#34;ContentCollectionViewCell&#34;, bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
    }


    // MARK - UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
      return getShop
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return 5
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; 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 = &#34;Number&#34;

                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 = &#34;Name&#34;

                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 = &#34;Phone&#34;

                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 = &#34;Email&#34;

                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 = &#34;Gender&#34;

                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
            }
      }
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我对你的问题有个想法。</p>

<p><strong>第 1 步</strong>:创建新结构以保存信息。</p>

<pre><code>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
    }
}
</code></pre>

<p><strong>第 2 步</strong>:
在<code>class tableViewController</code>中,新建变量:</p>

<p><code>var formData: [信息] = []</code></p>

<p><strong>第 3 步</strong>:每次用户在 tableView 的单元格中输入一个表单:</p>

<pre><code>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)
</code></pre>

<p><strong>第 4 步:</strong> 在 CollectionViewController 类中,声明新变量:</p>

<pre><code>var formData: = []
</code></pre>

<p><strong>第五步:</strong>为collectionView创建一个header:</p>

<p>|姓名 |电话 |电子邮件 |性别 |</p>

<p><strong>第 6 步:</strong>collectionView 中的每个单元格:</p>

<p>| <em>formData.name</em> | <em>formData.name</em> | <em>formData.name</em> | <em>formData.name</em> |</p>

<p><strong>第 7 步</strong>:</p>

<pre><code>@IBAction func submitButton(_ sender: Any) {
   mainController.formData = formData.
// do somethings
...
}
</code></pre>

<p>我不知道为什么你需要很多提交按钮来创建许多 Controller ,但让我们试试我的想法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 3 中将数据从所有 TableView Cell 传递到 CollectionView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42663510/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42663510/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 3 中将数据从所有 TableView Cell 传递到 CollectionView