我有一个名为“subcateory2”的视图crontroller,这个viewcontroller有一个uicollectionview wit自定义单元格。我的应用程序需要两个segues。从viewcontroller到其他视图控制器的其中一个名为“to_videostable”,另一个从单元格调用“reload_collection”到同一个viewcontroller(因为子类别可以有n级子类别)。问题出在我的prepareForSegue上(我在这个函数中检查了标识符,它在“func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath)”中定义,并执行不同的操作)。当我选择一个单元格时,这应该发生:
首先:转到我的“func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath)”,检查条件并定义我的segue标识符。
第二步:转到准备工作,检查条件并执行操作。
但实际上发生了这种情况:
第二个:我的代码转到prepareforsegue并且总是为segue调用“reload_collection”(在转到我的func collectionView(...)之前),并创建一个白色视图。在这一刻就像创建了两个线程,其中一个进入白色窗口,另一个进入下一站。
第三:这个“第二个理论”转到func collectionview(...)并检查条件,定义标识符,调用performSegueWithIdentifier并转到prepareforsegue函数。在prepareforsegue中检查标识符并执行不同的操作。
这是我的代码:
import UIKit
class Subcategory2: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let viewUtils = ViewControllerUtils()
var result_category: Array<JSON> = []
@IBOutlet weak var collectionview: UICollectionView!
var tit: String!
var url: String!
var end_url:String = "?page_size=100"
var id_category: Int!
var index: NSIndexPath!
var url_children : String = ""
let imagePath = "http://d1rkb03u2ginv9.cloudfront.net/wp-content/uploads/"
override func viewDidLoad() {
self.viewUtils.showActivityIndicator(self.view)
super.viewDidLoad()
self.viewUtils.showActivityIndicator(self.view)
if self.result_category.isEmpty{
var category = category_function()
self.url = self.url + self.end_url
self.result_category = category.load_subcategory(self.url)}
self.viewUtils.hideActivityIndicator(self.view)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
println("entroooooooo")
if (self.result_category[indexPath.row]["children"].string != nil){
self.url_children = self.result_category[indexPath.row]["children"].string!
//while(self.url_children.isEmpty){}
println("voy a reloadcollection")
performSegueWithIdentifier("reload_collection", sender: cell)
//performSegueWithIdentifier("reload_collection3", sender: self)
}else{
println("voy a to_videostables")
performSegueWithIdentifier("to_videostable", sender: cell)
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//println(result_category.count)
return result_category.count }
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell {
let cell: CollectionViewCellController2 = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! CollectionViewCellController2
println(self.result_category[indexPath.row]["slug"].stringValue)
cell.label.text = self.result_category[indexPath.row]["slug"].stringValue
if (self.result_category[indexPath.row]["images"]["file"].string != nil){
//println("+++++++++++++++++")
var image = self.result_category[indexPath.row]["images"]["file"].stringValue
cell.image.sd_setImageWithURL(NSURL(string:self.imagePath + (image as! String)))
}else{
var image = "http://www.camping-oaza.com/images/joomlart/demo/default.jpg"
cell.image.sd_setImageWithURL(NSURL(string: image))
//cell.image.image = UIImage(named: image)
}
cell.NumberVideosLabel.text = self.result_category[indexPath.row]["videos_count"].stringValue
cell.NumberSubcategoryLabel.text = self.result_category[indexPath.row]["children_count"].stringValue
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "to_videostable"{
println("-------------")
println("voy a to_videostables")
let cell = sender as! UICollectionViewCell
let index = self.collectionview!.indexPathForCell(cell) // this return -> NSIndexPath?
//if (self.result_category[index!.row]["children"].string != nil){
// self.loaddata(self.result_category[index!.row]["children"].string!)
//}else{
let vc : VideosViewController = segue.destinationViewController as! VideosViewController
println(self.result_category[index!.row]["id"].intValue)
vc.id_category = self.result_category[index!.row]["id"].intValue
}
if segue.identifier == "to_livevideos"{
println("-------------")
println("to_livevideos")
println("-------------------")
let vc : SWRevealViewController = segue.destinationViewController as! SWRevealViewController
}
if segue.identifier == "reload_collection"{
println("-------------")
println("reload_collection")
println("-------------------")
var category = category_function()
let vc : Subcategory2 = segue.destinationViewController as! Subcategory2
vc.url = self.url_children
println(category.load_subcategory(self.url_children + self.end_url))
}
}
}
有了这个问题,总是创建一个白色的窗口,之后创建一个带有真实信息的窗口。
println的顺序是: - “reload_collection” - “entroooooooo” - “voy a reloadcollection”或“voy a to_videostables”
答案 0 :(得分:0)
更新了答案
您可能希望在选择单元格时决定采用哪种segue。您已直接从单元格连接了一个segue,这意味着故事板将为您创建segue。您还调用performSegueWithIdentifier
来创建另一个segue。当你想要segue到“to_videostables”时,你需要实现shouldPerformSegueWithIdentifier
取消“reload_collection”segue。
在下面的原始答案中,我建议您从viewController连接两个segue,但这不起作用,因为您的一个segue返回到同一个viewController。
所以,另一种方法是:
修改didSelectItemAtIndexPath
以删除处理“reload_collection”segue的代码。故事板将成为那个segue:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
println("entroooooooo")
if result_category[indexPath.row]["children"].string == nil {
println("voy a to_videostables")
performSegueWithIdentifier("to_videostable", sender: cell)
}
}
将segue“reload_collection”从单元格连接到viewController。这将允许Storyboard为您执行此segue。
实施shouldPerformSegueWithIdentifier
告诉故事板应该何时进行此操作:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if segue.identifier == "reload_collection" {
let indexPath = collectionView.indexPathForCell(sender as! UICollectionViewCell)
return result_category[indexPath.row]["children"].string != nil
}
return true
}
在prepareForSegue
中,您需要设置url_children
,因为didSelectItemAtIndexPath
不再执行此操作:
if segue.identifier == "reload_collection"{
println("-------------")
println("reload_collection")
println("-------------------")
var category = category_function()
let vc : Subcategory2 = segue.destinationViewController as! Subcategory2
let indexPath = collectionView.indexPathForCell(sender as! UICollectionViewCell)
url_children = result_category[indexPath.row]["children"].string!
vc.url = url_children
println(category.load_subcategory(self.url_children + self.end_url))
}
原始答案
您的segue正在自动调用,因为您已从该单元连接它。如果您想使用performSegueWithIdentifier
触发它,则需要从viewController
开始连接,就像其他segue一样。只需从单元格中删除segue,然后从viewController重新连接它,并为它提供与从单元格连接时相同的标识符,它应该可以正常工作。