2015-06-07 51 views
0

我的工作我在一个教程中看到一个表视图的项目,然后我整个这一段代码,让我**error: Definition conflicts with previous value.**的TableView错误:titleForHeaderInSection /斯威夫特

的一段代码来为:

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

} 

我试图改变字符串?到String或Int中,但String给了我相同的错误,Int给了我一个返回行上的错误。

这里是我的完整代码:

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, numberOfRowsInSection section: Int, 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 

    // Configure the cell... 
    cell.textLabel?.text = animals[indexPath.row] 

    // Convert the animal name to lower case and 
    // then replace all occurences of a space with an underscore 
    let imageFilename = animals[indexPath.row].lowercaseString.stringByReplacingOccurrencesOfString(" ", withString: "_", options: nil, range: nil) 
    cell.imageView?.image = UIImage(named: imageFilename) 

    return cell 
} 

回答

1

两件事情:


1)

你应该改变你的线

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

目前,它从第二个子串字符w这意味着对于输入Black Swan,那么animalKey将等于lack Swan。相反,你应该使用下面的行:

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

2)

有一个在UITableViewDataSource Protocol没有一种方法被称为tableView:numberOfRowsInSection:titleForHeaderInSection。相反,你需要把它分为以下两种方法:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let title = animalSelectionTitles[section] 
    return animalsDict[title]!.count 
} 

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

更新1:

在你tableView:cellForRowAtIndexPath,你也应该更新动物名称的检索,以反映存储在字典里如此:

// Configure the cell... 
let secTitle = animalSelectionTitles[indexPath.section] 
let animalName = animalsDict[secTitle]![indexPath.row] 
cell.textLabel?.text = animalName