迅捷排行榜

时间:2018-10-11 02:11:43

标签: swift append leaderboard

我试图制作一个排行榜,并从另一个视图控制器追加,并且显示了名称,但是由于某种原因,无论我追加的分数是多少,分数总是显示为0。有人可以帮我看看我的代码有什么问题吗?

导入基金会 导入UIKit 类页首横幅:UITableViewController {

var players: [String] = []
var scores:[Int] = []
var gameFinished = false
var allMyData = UserDefaults.standard
var player = ""
var score = 0

override func viewDidLoad() {

 loadData()

    if gameFinished {

        if player == "" {
            player = "player"
        }

        players.append(player)
        scores.append(score)
    }

    players = bubbleSort(player: players, score: scores).0
  scores = bubbleSort(player: players, score: scores).1
   saveData()

}

func bubbleSort (player: [String] , score: [Int]) -> ([String],[Int]) {

    var players1 = player
    var scores1 = score

        var isSwapped = true

        while isSwapped == true {
            isSwapped = false
            if players1.count != 0 {
                for index in 1..<players1.count {
                    if scores1[index] > scores1[index - 1] {
                        scores1.swapAt(index - 1, index)
                        players1.swapAt(index - 1, index)

                        isSwapped = true
                    }

                }

            }

            }
     return (players1, scores1)
        }

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

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Leaderboard"
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "myCoolCell")
    myCell.textLabel?.text = "\(players[indexPath.row])"
    myCell.detailTextLabel?.text = "\(scores[indexPath.row])"
    return myCell
}

func saveData() {

    allMyData.set(players, forKey: "saveMyName")
    allMyData.set(scores, forKey: "saveMyNumber")
}

func loadData() {

    if let loadMyScore:[Int] = allMyData.value(forKey: "saveMyNumber") as? [Int] {
        scores = loadMyScore      
    }
    if let loadMyName:[String] = allMyData.value(forKey: "saveMyName") as? [String] {
       players = loadMyName

    }

}

}

0 个答案:

没有答案