【问题标题】:Table View UI error: Swift表视图 UI 错误:Swift
【发布时间】:2015-06-14 11:35:35
【问题描述】:

关于上一个问题,我询问了动物表视图项目中的代码错误,现在我完成了初始编码,但我的 UI 变得非常奇怪。它缺少每个动物名称的第一个字母和表格视图原型单元格。 例如,amel 应该是骆驼,hinoceros 应该是犀牛。 这是这里代码的错误吗?

import UIKit

class AnimalTableViewController: UITableViewController {

var animalsDict = [String: [String]] ()
var animalSelectionTitles = [String] ()

let animals = ["Bear", "Black Swan", "Buffalo", "Camel", "Cockatoo", "Dog", "Donkey", "Emu", "Giraffe", "Greater Rhea", "Hippopotamus", "Horse", "Koala", "Lion", "Llama", "Manatus", "Meerkat", "Panda", "Peacock", "Pig", "Platypus", "Polar Bear", "Rhinoceros", "Seagull", "Tasmania Devil", "Whale", "Whale Shark", "Wombat"]

func createAnimalDict() {
    for animal in animals {
        let animalKey = animal.substringFromIndex(advance(animal.startIndex, 1))
        if var animalValues = animalsDict[animalKey] {
            animalValues.append(animal)
            animalsDict[animalKey] = animalValues
        } else {
            animalsDict[animalKey] = [animal]
        }
    }
    animalSelectionTitles = [String] (animalsDict.keys)
    animalSelectionTitles.sort({ $0 < $1})
    animalSelectionTitles.sort( { (s1:String, s2:String) -> Bool in
        return s1 < s2
    })
}

override func viewDidLoad() {
    super.viewDidLoad()

    createAnimalDict()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
    return animalSelectionTitles.count
}

override func tableView(tableView: UITableView, titleForHeaderInSection section:Int)  -> String? {
    // Return the number of rows in the section.
    return animalSelectionTitles[section]

}


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

    let animalKey = animalSelectionTitles[indexPath.section]
    if let animalValues = animalsDict[animalKey] {
        cell.textLabel?.text = animalValues[indexPath.row]
        let imageFileName = animalValues[indexPath.row].lowercaseString.stringByReplacingOccurrencesOfString("", withString: "_", options: nil, range: nil)
        cell.imageView?.image = UIImage(named:imageFileName)
    }
    return cell
}

}

【问题讨论】:

  • 您是否检查过animalsDict 包含您所期望的内容?不应该是substringToIndex,而不是substringFromIndex,正如您在上一个问题的答案中所建议的那样吗?您是否在titleForHeaderInSection cellForRowAtIndexPath 中设置了断点并使用调试器 检查“错误”名称的来源?

标签: swift uitableview user-interface xcode6


【解决方案1】:

到目前为止,我可以说错误出在您的 createAnimalDict() 方法中。在行中

let animalKey = animal.substringFromIndex(advance(animal.startIndex, 1))

把第二个参数提前换成0这样:

let animalKey = animal.substringFromIndex(advance(animal.startIndex, 0))

事实上,我真的不知道你想做什么。

【讨论】:

    【解决方案2】:

    在这个方法中:

    override func tableView(tableView: UITableView, titleForHeaderInSection section:Int)  -> String? {
        // Return the number of rows in the section. (THIS COMMENT IS INCORRECT)
        return animalSelectionTitles[section]
    }
    

    您将返回每个部分的标题。但是,您的 animalSelectionTitles[index] 包含不带首字母的动物名称,因为它在 createAnimalDict 中构造

    使用动物数组代替提供完整的动物名称:

    override func tableView(tableView: UITableView, titleForHeaderInSection section:Int)  -> String? {         
         return animals[section]
    }
    

    但是请注意,由于删除了第一个字母,您可能有两个动物映射到同一个键,因此如果没有必要,请使用整个动物名称作为键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多