2017-03-08 62 views
2

我已經創建了動態表單,用戶輸入了多個表單,併爲此創建了表格單元格。提交所有表單後,所有細節都應顯示在excel formate中,因爲我已創建集合視圖,但問題是唯一選中的單元格值正在打印。如何將數據從所有TableViewCell傳遞到Swift3中的CollectionView

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 
      } 
     } 
    } 
+0

說真的。你應該保持你的代碼清潔。 – javimuu

+0

@javimuu我認識的人!因爲我有很多方法可以解決這個問題。你能幫我嗎?我可以寄給你所有的代碼。 – ViralRathod

回答

0

我對你的問題的想法。

步驟1:創建新的結構來保存信息。

​​

步驟2: 在class tableViewController,創建新的變量:

var formData: [Information] = []

步驟3:每次用戶在的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) 

步驟4:在類CollectionViewController,聲明新的變量:

var formData: [Information] = [] 

步驟5:爲的CollectionView創建標題:

|名稱|電話|電子郵件|性別|

步驟6:在的CollectionView每個小區:

| formData [indexPath] .name | formData [indexPath] .name | formData [indexPath] .name | formData [indexPath] .name |

步驟7

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

我不知道爲什麼你需要大量的提交按鈕創建多個控制器,但讓嘗試我的想法。

+0

感謝您的幫助thx。我在tableView控制器中創建了第二個按鈕,用於選擇所有行 – ViralRathod

+0

我可以收到您的電子郵件,因此我向您發送所有代碼,然後您可以得到確切的問題? – ViralRathod