【问题标题】:Type [TeamModel] has no subscript members类型 [TeamModel] 没有下标成员
【发布时间】:2016-08-03 20:04:14
【问题描述】:

我正在尝试从 UICollectionView 单元到另一个 viewController 执行 segue,但是当我尝试传递数据时,Xcode 向我发送此错误“类型 [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]
    }
}

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

【问题讨论】:

  • teamsArrayTeamModel 长什么样子?
  • 为什么teamsArray[] 中的prepareForSegue() 方法中有问号作为索引?您需要首先从数组中取出相关的TeamModel,然后将chant 值从该TeamModel 分配给controller.chant
  • 我刚刚做了,但是编译器将我在标题中写的错误发送给我。

标签: ios swift segue subscript


【解决方案1】:

如评论问题

indexpath[0].row insted indexpath.row:你能解释一下吗?

你正在使用方法

collectionView.indexPathsForSelectedItems()

它返回一个数组,就这么简单,所以你需要使用

DataManager.sharedInstance.teamsArray[indexPath[0].row]

【讨论】:

    【解决方案2】:

    如果你像这样填充和声明teamArray:

    var teamsArray: [TeamModel] = []
    
    teamsrray.append(juventus)
    teamsrray.append(milan)
    ...
    

    它应该可以工作

    【讨论】:

    • 所以这是一组 TeamModel 项目?告诉我你是如何设置 teamArray 的 :)
    • 解释起来有点长 :) 在我要发布 TeamModel 类和 DataManager 时
    • 请检查编辑,还有一件事,controller.chant 是 ChantModel 还是 TeamModel?
    • 是一个 TeamModel 对象;在数组索引中,如果我输入某个数字(例如 0),应用程序应该可以工作
    • 刚试过,编译器说[indexPath]没有成员行。
    猜你喜欢
    • 2021-06-05
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多