【发布时间】:2014-07-08 03:33:58
【问题描述】:
我正在尝试在 Swift 中创建一个非常基本的集合视图控制器,它允许我列出一些数字,当我点击其中一个时,它会将我带到一个详细信息页面,该页面简单地说明(在标签中)你是什么数字轻拍。
故事板: 我的故事板有一个作为根的导航控制器和一个加载的集合视图控制器。这包含一个单元格,其标识符为单元格。此单元格有一个按钮,用作详细信息页面的 segue。我已将数据源分配为指向自身(控件单击视图控制器的行以建立数据源)并且我已将 segue 唯一标识为 showDetail。
当我加载应用程序时,它会显示我的按钮 7 次,但是当我单击其中任何一个时,我会在下面的行中得到一个超出范围的索引:
这是我的代码:
import Foundation
import UIKit
class WeekCollectionView : UICollectionViewController
{
override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return 7
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
let cell: ShiftCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as ShiftCell
return cell;
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "showDetail") {
**//CRASHES ON THIS LINE BELOW FOR THE ARRAY [0]**
let indexPaths: NSArray = self.collectionView.indexPathsForSelectedItems()[0] as NSArray
let destinationViewController = segue.destinationViewController as DetailViewController
//var someNumber = images[indexPath.row]
destinationViewController.mySpecialID = 1 //replace this with someNumber, always set 1 for now as a test
}
}
}
我的单元格类(上图)很简单:
class ShiftCell: UICollectionViewCell {
init(frame: CGRect) {
super.init(frame: frame)
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
}
我真的不确定我做错了什么 - 也许我的故事板中需要发生其他事情?我遵循了来自苹果的教程(在objective-c中),它运行良好,如果我没记错的话,所有的代码都是相同的!
我真的被困在这里,如果有人能指出我正确的方向,非常感谢! :)
【问题讨论】:
标签: ios ios7 swift xcode-storyboard