2015-06-07 48 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