我正试图执行从UICollectionView单元格到其他viewController的segue,但当我试图传递数据时,Xcode向我发送此错误“Type[TeamModel]没有下标成员”。
全班同学都来了

    import UIKit

class AllTeamController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
    // MARK: properties

    let navigationBar = UINavigationBar()
    let segmentedControl = ADVSegmentedControl()
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var menuButton: UIBarButtonItem!

    // MARK: view method

    override func viewDidLoad()
    {
        super.viewDidLoad()

        DataManager.sharedInstance.loadTeams()

        collectionView!.backgroundColor = UIColor.returnBasicColor("clearGreen")
        collectionView!.decelerationRate = UIScrollViewDecelerationRateFast

        let nib = UINib(nibName: "TeamCell", bundle: nil)
        collectionView.registerNib(nib, forCellWithReuseIdentifier: "teamCell")
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    // MARK: collection view data source and delegate

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return DataManager.sharedInstance.teamsArray.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell

        let team = DataManager.sharedInstance.teamsArray[indexPath.row]
        cell.teamImage.image = UIImage(named: team.teamImage)
        cell.teamName.text = team.teamName
        cell.teamAdress.text = team.teamAdress
        cell.teamNumberOfChants.text = "\(team.teamNumeberOfChants)"

        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
        performSegueWithIdentifier("toChantController", sender: self)
    }

    // MARK: navigation (segue)

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "toChantController")
        {
            if let indexPath = collectionView.indexPathsForSelectedItems()
            {
                let controller = segue.destinationViewController as! TeamChantViewController
                controller.chant = DataManager.sharedInstance.teamsArray[??????????????????]
            }
        }
    }

DataManager
  import UIKit

class DataManager: NSObject
{
    class var sharedInstance:DataManager
        {
        get {
            struct Static
            {
                static var instance : DataManager? = nil
                static var token : dispatch_once_t = 0
            }
            dispatch_once(&Static.token) { Static.instance = DataManager() }

            return Static.instance!
        }
    }

    // MARK: properties

    var teamsArray: [TeamModel]!
    var settingsArray: [[(name: String, description: String, image: String)]]!

    // MARK: methods

    func loadTeams()
    {
        let juveChant1 = ChantModel(name: "1")
        let juveChant2 = ChantModel(name: "2")
        let juveChant3 = ChantModel(name: "3")
        let juventus = TeamModel(name: "Juventus", adress: "Cori della Juventus", number: 34, colors: ("juvColor","juvColor"), image: "juve_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let milan = TeamModel(name: "Milan", adress: "Cori del Milan", number: 12, colors: ("milColor","mlnColor"), image: "milan_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let inter = TeamModel(name: "Inter", adress: "Cori dell'Inter", number: 19, colors: ("intColor","intColor"), image: "inter_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let roma = TeamModel(name: "Roma", adress: "Cori della Roma", number: 10, colors: ("romColor","romColor"), image: "roma_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let napoli = TeamModel(name: "Napoli", adress: "Cori del Napoli", number: 21, colors: ("napColor","napColor"), image: "napoli_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let lazio = TeamModel(name: "Lazio", adress: "Cori della lazio", number: 5, colors: ("lazColor","lazColor"), image: "lazio_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let fiorentina = TeamModel(name: "Fiorentina", adress: "Cori della Fiorentina", number: 9, colors: ("fioColor","fioColor"), image: "fiorentina_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        let torino = TeamModel(name: "Torino", adress: "Cori del Torino", number: 11, colors: ("torColor","torColor"), image: "torino_ritaglio", chants: [juveChant1,juveChant2,juveChant3])
        teamsArray = [juventus,milan,inter,roma,napoli,lazio,fiorentina,torino]
    }

    func loadSettings()
    {
        let media = [(name: "Preferiti", description: "I tuoi cori prefriti", image: "heartIcon"),(name: "Stadi d'Italia", description: "Galleria degli stadi italiani", image: "stadiumIcon"),(name: "Mappa", description: "Luoghi di interesse", image: "mapIcon")]
        let social = [(name: "Facebook", description: "Visita la pagina Facebook di iSupporters", image: "facebbokIcon"),(name: "Google+", description: "Visita la pagina Google+ di iSupporters", image: "google+Icon"),(name: "Twitter", description: "Visita la pagina Twitter di iSupporters", image: "twitterIcon"),(name: "Diffondi", description: "Suggerisci iSupporters a un amico", image: "megaphoneIcon")]
        let settings = [(name: "Impostazioni", description: "Impostazioni e restrizioni", image: "settingImage"),(name: "Supporto", description: "Segnala un problea su iSupporters", image: "supportIcon"),(name: "Vota", description: "Dai un voto ad iSupporters su App Store", image: "rateIcon")]
        settingsArray = [media,social,settings]
    }
}

希望有人能解释我哪里做错了!

最佳答案

如评论问题
indexpath[0].row insted indexpath.row:能解释一下吗?
你在用方法

collectionView.indexPathsForSelectedItems()

它返回一个数组,非常简单,所以您需要使用
DataManager.sharedInstance.teamsArray[indexPath[0].row]

关于ios - 类型[TeamModel]没有下标成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38729416/

10-12 14:35