UITableView单元格选择错误:单元格更改文本,无法取消选择行?

时间:2014-12-15 22:21:16

标签: uitableview swift selected deselect

我尝试使用UITableView在swift中创建一个简单的匹配游戏,您必须将船型与该船型的定义相匹配。基本上,现在一切都搞砸了游戏。单元格的样式设置为 plain 。以下是可能导致问题的方法的代码:

var currentSelectedText = "None"
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    switch cell.textLabel.text! as String {
    case "Used to carry the pharoah":
        currentSelectedText = "Used to carry the pharoah"
    case "Used to carry bodies as a ceremony":
        currentSelectedText = "Used to carry bodies as a ceremony"
    case "Had a flat deck to carry a farmer's treasure":
        currentSelectedText = "Had a flat deck to carry a farmer's treasure"
    case "Daily, it made a trip around the world to carry Ra":
        currentSelectedText = "Daily, it made a trip around the world to carry Ra"
    case "Towed by smaller boats, carrying heavy objects":
        currentSelectedText = "Towed by smaller boats, carrying heavy objects"
    case "Used for business and pleasure by officials/nobles":
        currentSelectedText = "Used for business and pleasure by officials/nobles"
    case "Carried most Egyptians and some goods":
        currentSelectedText = "Carried most Egyptians and some goods"
    default:
        currentSelectedText = "None"
    }
    switch indexPath.row {
    case 7:
        switch cell.textLabel.text! as String {
        case "Ferry":
            if currentSelectedText == "Carried most Egyptians and some goods" {
                cell.textLabel.text = "Correct"
                cell.textLabel.textColor = UIColor.greenColor()
            } else if currentSelectedText == "None" {
            } else {
                performSegueWithIdentifier("Game End", sender: self)
            }
        case "Funeral Barge":
            if currentSelectedText == "Used to carry bodies as a ceremony" {
                cell.textLabel.text = "Correct"
                cell.textLabel.textColor = UIColor.greenColor()
            } else if currentSelectedText == "None" {
            } else {
                performSegueWithIdentifier("Game End", sender: self)
            }
        case "Cargo Boat":
            if currentSelectedText == "Towed by smaller boats, carrying heavy objects" {
                cell.textLabel.text = "Correct"
                cell.textLabel.textColor = UIColor.greenColor()
            } else if currentSelectedText == "None" {
            } else {
                performSegueWithIdentifier("Game End", sender: self)
            }

我有更多的情况,一直到第13行(case 8, case 9, case 10 . . .)。我也有更多的船型(我有7种船型)。我还有一个取消选择行的方法:

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    currentSelectedText = "None"
}

请记住,单元格样式设置为 plain 。问题是:

  1. 单元格的文本更改为" Title"当我选择它。
  2. 我无法取消选择行
  3. 如果还有其他任何不适合使用UITableView的匹配游戏的话,如果你列出我可以做些什么来解决这个问题,我将不胜感激。
  4. 如果我需要提供更多代码,请告知我们。谢谢!

0 个答案:

没有答案