2016-10-04 108 views
0

我曾在變量球員的錯誤,我發現故障一直未能完成使用未聲明的類型「球員」

import UIKit 

class PlayersViewController: UITableViewController { 

var players:[Player] = playersData 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return players.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 
    -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("PlayerCell", forIndexPath: indexPath) 

     let player = players[indexPath.row] as Player 
     cell.textLabel?.text = player.name 
     cell.detailTextLabel?.text = player.game 
     return cell 
    } 

} 

enter image description here

+1

你還沒有定義玩家類型。什麼是玩家類型應該是什麼? – pedrouan

回答

0

您需要首先定義Player類型。你可能會想到這樣的事情:

class PlayersViewController: UITableViewController { 

    var players: [Player] = [] 
    var xplayers: [XPlayer] = [] 

    // struct type of Player 
    struct Player { 
     var name : String = "" 
     var height : Int = 0 
    } 

    // OR 

    // tuple type of XPlayer 
    typealias XPlayer = (String, Int)  

    func someMethod() { 

     self.players.append(Player(name: "John Herbert", height: 160)) 

     // OR 

     self.xplayers.append(("John Herbert", 160)) 

    } 
} 
相關問題