2017-08-21 3 views
-1

これで私は最後のセクションが必要な3つのセクションがありました。tableviewcellデータはテーブルビュークラスに渡す必要がありますが、nsnotificationcenterを使用してデータを渡すことを考えていましたが、誰もがuitableの中IBActionを作成するのではなく、nsnotificationテーブルビューセルクラスからビューコントローラテーブルビュークラスにデータを渡すにはどうすればいいですか?

表ビュークラス

override func viewDidLoad() { 
     super.viewDidLoad() 
     _ = UIApplication.shared.statusBarOrientation 
     tableDetails.isHidden = true 
     continueButton.layer.cornerRadius = 5 
     myActivityIndicator.frame = CGRect(x: 130, y: 320, width: 30, height: 30) 
     myActivityIndicator.hidesWhenStopped = true 
     myActivityIndicator.startAnimating() 
     view.addSubview(myActivityIndicator) 
     myActivityIndicator.translatesAutoresizingMaskIntoConstraints = false 
     let horizontalConstraint = NSLayoutConstraint(item: myActivityIndicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0) 
     view.addConstraint(horizontalConstraint) 
     let verticalConstraint = NSLayoutConstraint(item: myActivityIndicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0) 
     view.addConstraint(verticalConstraint) 
     self.shippingaddressURL() 
     tableDetails.delegate = self 
     tableDetails.dataSource = self 
     tableDetails.separatorInset = UIEdgeInsets.zero 
     tableDetails.rowHeight = UITableViewAutomaticDimension 
     tableDetails.estimatedRowHeight = 50 
     self.title = "Checkout" 
     // Do any additional setup after loading the view. 
    } 
     func shippingaddressURL() { 
     let url = NSURL(string: self.url) 
     URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in 
      if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 
      self.shippingArray = (jsonObj!.value(forKey: "address") as? [[String: AnyObject]])! 
       OperationQueue.main.addOperation({ 
        self.tableDetails.reloadData() 
       }) 
      } 
     }).resume() 
    } 
    @IBAction func selectRadioButton(_ sender: KGRadioButton) { 
     let chekIndex = self.checkIsRadioSelect.index(of: sender.tag) 
     _ = self.checkIsButtonEnable.index(of: sender.tag) 
     if sender.isSelected { 

     } else{ 
      if(chekIndex == nil){ 
       self.checkIsRadioSelect.removeAll(keepingCapacity: false) 
       self.checkIsRadioSelect.append(sender.tag) 
       self.checkIsButtonEnable.removeAll(keepingCapacity: false) 
       self.checkIsButtonEnable.append(sender.tag) 
       self.tableDetails.reloadData() 
      } 
     } 
    } 
    func numberOfSections(in tableView: UITableView) -> Int{ 
     return 3 
    } 
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     if (section == 0){ 
      return "SHIPPING ADDRESS" 
     } 
     else if (section == 2){ 
      return "SHIPPING METHOD" 
     } 
     else{ 
      return "" 
     } 
    } 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 
     if (indexPath.section == 0){ 
      return UITableViewAutomaticDimension 
     } 
     else if (indexPath.section == 1){ 
      return 62 
     } 
     else { 
      return 282 
     } 
    } 

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){ 
     let header = view as! UITableViewHeaderFooterView 
     header.textLabel?.textColor = UIColor.gray 
     header.textLabel?.textAlignment = NSTextAlignment.center 
     header.textLabel?.font = UIFont(name: "Futura", size: 17) 
    } 

    @IBAction func newAddressAction(_ sender: Any) { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "newAddress") as! NewAddressViewController 
     self.navigationController?.pushViewController(addtoCartVC, animated: true) 
    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
     if (section == 0){ 
      return shippingArray.count 
     } 
     else { 
      return 1 
     } 
    } 
    @IBAction func continueButtonAction(_ sender: Any) { 
     if selected == false{ 
      let radiobutton = SCLAlertView() 
      _ = radiobutton.showError("Warning", subTitle: "Please select shipping method", closeButtonTitle: "OK") 

     }else{ 
      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "payment") as! PaymentMethodViewController 
      self.navigationController?.pushViewController(addtoCartVC, animated: true) 
     } 
    } 
    @IBAction func deleteAction(_ sender: UIButton) { 
     shippingArray.remove(at:sender.tag) 
     self.tableDetails.reloadData() 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     if (indexPath.section == 0) 
     { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddressTableViewCell 
      tableDetails.isHidden = false 
      myActivityIndicator.stopAnimating() 
      let arr = shippingArray[indexPath.row] 
      cell.deleteButton.tag = indexPath.row 
      cell.nameLabel.text = arr["name"] as? String 
      cell.addressLabel.text = arr["address"]as? String 
      let mobilenumber : Any = arr["number"] as AnyObject 
      cell.mobileNumberLabel.text = "\(mobilenumber)" 
      cell.radioButton.tag = indexPath.row 
      cell.editButton.tag = indexPath.row 
      cell.deleteButton.tag = indexPath.row 
      cell.editButton.isHidden = true 
      cell.deleteButton.isHidden = true 
      let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row) 
      if(checkIndex != nil){ 
       cell.radioButton.isSelected = true 
       cell.editButton.isHidden = false 
       cell.deleteButton.isHidden = false 
      }else 
      { 
       cell.radioButton.isSelected = false 
       cell.editButton.isHidden = true 
       cell.deleteButton.isHidden = true 
      } 
      return cell 
     } 
     else if (indexPath.section == 1){ 
      let cell = tableView.dequeueReusableCell(withIdentifier: "addresscell", for: indexPath) as! CreateNewAddressTableViewCell 
      cell.newAddressButton.addTarget(self, action: #selector(newAddressAction(_:)), for: .touchUpInside) 
      return cell 
     } 
     else { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "shippingmethodcell", for: indexPath) as! MethodTableViewCell 
      return cell 
     } 
    } 

テーブルビューセルクラス

var chekIndex:IndexPath? 
    var arrayss = [String:AnyObject]() 
    var keys = [String]() 
    let urlString = "http://www.json-generator.com/api/json/get/bVgbyVQGmq?indent=2" 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.shippingmethodURL() 
     shippingTableView.delegate = self 
     shippingTableView.dataSource = self 
     shippingTableView.rowHeight = UITableViewAutomaticDimension 
     shippingTableView.estimatedRowHeight = shippingTableView.rowHeight 
     // Initialization code 
    } 
    @IBAction func paymentRadioAction(_ sender: KGRadioButton) { 
     _ = sender.center 
     let centralPoint = sender.superview?.convert(sender.center, to:self.shippingTableView) 
     let indexPath = self.shippingTableView.indexPathForRow(at: centralPoint!) 
     if sender.isSelected { 

     } else{ 
      chekIndex = indexPath 
      isSelected = true 
      self.shippingTableView.reloadData() 
     } 
    } 
    func shippingmethodURL() { 
     let url = NSURL(string: self.urlString) 
     URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in 
      if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 
       self.arrayss = jsonObj as! [String : AnyObject] 
       print(self.arrayss) 
       self.keys = jsonObj?.allKeys as! [String] 
       print(self.keys) 
       OperationQueue.main.addOperation({ 
        self.shippingTableView.reloadData() 
        let sectionHeight = self.arrayss.count * 31 
        let cellHeight = self.keys.count * 44 
        self.shippingHeightConstraint.constant = CGFloat(sectionHeight + cellHeight) 
       }) 
      } 
     }).resume() 
    } 


    func numberOfSections(in tableView: UITableView) -> Int{ 
      return arrayss.count 
     } 
     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
      return self.keys[section] 
     } 
     func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){ 
      let header = view as! UITableViewHeaderFooterView 
      header.tintColor = UIColor.white 
      header.textLabel?.textColor = UIColor.darkGray 
      header.textLabel?.textAlignment = NSTextAlignment.left 
      header.textLabel?.font = UIFont(name: "Futura", size: 17) 
     } 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
      let key = self.keys[section] 
      let a :[Any] = arrayss[key] as! [Any] 
      return a.count 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "shippingCell", for: indexPath) as! ShippingMethodTableViewCell 
      let bgColorView = UIView() 
      bgColorView.backgroundColor = UIColor.white 
      cell.selectedBackgroundView = bgColorView 
      let key = self.keys[indexPath.section] 
      var a :[Any] = arrayss[key] as! [Any] 
      var dictionary = a[indexPath.row] as! [String:Any] 
      let name = dictionary["name"] 
      let price = dictionary ["price"] 
      cell.methodLabel.text = name as? String 
      cell.priceLabel.text = price as? String 
      cell.radioButton.addTarget(self, action: #selector(paymentRadioAction), for: .touchUpInside) 
      if chekIndex == indexPath { 
       cell.radioButton.isSelected = true 
      } else { 
       cell.radioButton.isSelected = false 
      } 
      return cell 
     } 
+0

理由通知?要件は1対多ですか?もしそうでなければ、私は1対1のハンドオフ(代表者)を好むだろう。 –

+0

可能であれば、代理人も問題ありません@TusharSharma –

+0

私は1対1しか必要ありません。つまり、テーブルビューのセルクラスからテーブルビューのクラス@TusharSharma –

答えて

0

を使用してデータをポストする方法を私を助けることができますその後、

cell.radioButton.addTarget(self, action: #selector(YourViewController.someMethod(_sender:)), for: UIControlEvents.touchUpInside) 

そして、あなたは文句を言わないデータを渡すために持っているように、ViewControllerを自身であなたのAPIを呼び出す: - - :ViewCellクラス、cellForRowメソッドでViewControllerクラスに、ボタンのためのaddTarget

func someMethod(_sender: UIButton) 
{ 
    // Call your Api here in View Controller Class 
} 
+0

テーブルビューのセルでself.shippingHeightConstraint.constantの値を取得して、テーブルビューのクラスで使用する必要がありますか?@Ashish Pisey –

+0

self.shippingHeightConstraint.constant = CGFloat(sectionHeight + cellHeight)この値は、テーブルビュークラスで取得する必要があります。@Ashish Pisey –

+0

しかし、テーブルビュークラスのデータをロードする必要があります。私のコード –

関連する問題