集合视图单元格重定向不同的 ViewController
Posted
技术标签:
【中文标题】集合视图单元格重定向不同的 ViewController【英文标题】:Collection view Cells redirecting different ViewController 【发布时间】:2017-04-03 11:01:15 【问题描述】:我有 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)
上面的代码我在点击时显示一个警报。
【问题讨论】:
***.com/a/41887007/5461400 感谢 HarshalValanda。 欢迎 ck1924...... 【参考方案1】:如果我正确理解了您的问题,那么一种方法是在情节提要中为每个 UIViewController 设置标识符,然后在您的 didSelectItemAt 中调用它:
let vc = self.storyboard?.instantiateViewController(withIdentifier: sectionArr[indexPath.section]) as! UIViewController
self.present(vc, animated: true, completion: nil)
【讨论】:
【参考方案2】:如果您使用的是 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
【讨论】:
谢谢 jignesh Vadadoriya ...您的回答有帮助以上是关于集合视图单元格重定向不同的 ViewController的主要内容,如果未能解决你的问题,请参考以下文章