我有UICollectionView
节。每个部分都有由数组计数定义的可变单元格。我已经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)
}
}
上面的代码我在单击时显示警报。
如果我正确理解了您的问题,那么一种方法是为情节提要中的每个UIViewController设置标识符,然后在didSelectItemAt中调用它:
let vc = self.storyboard?.instantiateViewController(withIdentifier: sectionArr[indexPath.section]) as! UIViewController
self.present(vc, animated: true, completion: nil)
如果您
使用的是 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
}
}