2017-04-03 40 views
0

我有UICollectionView節。每個部分都有由陣列數量定義的可變單元格。我已經爲viewDidLoad函數中的每個部分聲明瞭字符串數組。集合視圖單元重定向不同的ViewController

我希望每個單元打開一個新的UIViewController點擊相應的單元格。我如何得到上述結果。

我正在使用Swift3進行編碼。

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    @IBOutlet weak var collectView: UICollectionView! 

    var sectionHeader = [String]() 

    var sectionArr = [Any]() 
    var enquire = [String]() 
    var serviceRequest = [String]() 
    var missedCall = [String]() 
    var settings = [String]() 

    var arr = [String]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     sectionHeader = ["Enquire","Service Request","Missed Call Recharge","Settings"] 

     enquire = ["Balance Enquiry","Mini Statement","Account Statement","Cheque Status Enquiry","Fixed Deposit Enquiry"] 
     serviceRequest = ["Stop Cheque Payment","Cheque Book Request","iPIN Regeneration"] 
     missedCall = ["Activate","Deactivate","Set Recharge Amount","Recharge"] 
     settings = ["Change Primary Account","Register"] 

     sectionArr = [enquire,serviceRequest,missedCall,settings] 

     collectView.dataSource = self 
     collectView.delegate = self 

     collectView.reloadData() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func loadCollectionView(_ sender: Any) { 
     collectView.reloadData() 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return sectionArr.count 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     if (section == 0) { 
      return enquire.count 
     } 
     else if (section == 1) { 
      return serviceRequest.count 
     } 
     else if (section == 2) { 
      return missedCall.count 
     } 
     else { 
      return self.settings.count 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let mycell:CustomCell = collectView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CustomCell 

     arr = sectionArr[indexPath.section] as! [String] 

     let imagename = arr[indexPath.row] 

     let modified = imagename.replacingOccurrences(of: " ", with: "_") 

     let modified_imagename = modified + ".png" 

     mycell.functionalityImage.image = UIImage(named : modified_imagename) 
     mycell.functionalityName.text = arr[indexPath.row] 

     return mycell 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     arr = sectionArr[indexPath.section] as! [String] 

     showAlert(mesgTitle: "SELECTED CELL", mesgText: arr[indexPath.row]) 
    } 

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

     var reusea:UICollectionReusableView? = nil 

     if (kind == UICollectionElementKindSectionHeader) { 

      let header:HeaderTextHome = collectView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header_Text", for: indexPath) as! HeaderTextHome 

      header.headerText.text = sectionHeader[indexPath.section ] 

      reusea = header 

     } 
     return reusea! 
    } 

    func showAlert(mesgTitle : String, mesgText : String) { 

     let alertController = UIAlertController(title: mesgTitle, message: mesgText, preferredStyle: UIAlertControllerStyle.alert) 

     let defaultAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil) 

     let cancleAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil) 

     alertController.addAction(defaultAction) 
     alertController.addAction(cancleAction) 

     present(alertController, animated: true, completion: nil) 

    } 

} 

上面的代碼我點擊顯示警報。

+0

http://stackoverflow.com/a/41887007/5461400 –

+0

感謝HarshalValanda。 – ck1924

+0

歡迎ck1924 ...... –

回答

0

如果我理解正確的問題,那麼這樣做是設置在你的故事板的每個UIViewController的標識,然後在didSelectItemAt調用此方法之一:

let vc = self.storyboard?.instantiateViewController(withIdentifier: sectionArr[indexPath.section]) as! UIViewController 
self.present(vc, animated: true, completion: nil) 
0

如果您正在使用SEGUE然後設置所有viewController segue in storyboard然後使用波紋管代碼。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     arr = sectionArr[indexPath.section] as! [String] 
     let viewControllerIdentifire = arr[indexPath.item] 
     self.performSegue(withIdentifier: viewControllerIdentifire, sender: IfYouWantToPassDataThanPutHereElseNil) 
     // showAlert(mesgTitle: "SELECTED CELL", mesgText: arr[indexPath.row]) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "Enquire" { 
      let vc = segue.destinationViewController as! EnquireViewController 
      vc.data = sender // if you want to pass data to viewCotroller 
     } 
    } 
+0

謝謝jignesh Vadadoriya ...您的回答幫助 – ck1924

+0

然後投票我的答案 –

相關問題