集合视图单元重定向不同的ViewController

时间:2017-04-03 11:01:16

标签: ios iphone swift3 uicollectionview ios8.3

我有UICollectionView部分。每个Sections都有可变单元格,由数组计数定义。我已经为viewDidLoad函数中的每个部分声明了字符串数组。

我希望每个单元格在单击相应单元格时打开新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)

    }

}

以上代码我点击时会显示警告。

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么一种方法是在故事板中为每个UIViewController设置标识符,然后在didSelectItemAt中调用它:

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

答案 1 :(得分:0)

如果你正在使用segue然后在故事板中设置所有viewController segue,那么请使用以下代码。

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