Вы игнорируете номер раздела по пути указателя, который вы получаете от tableView.indexPathForSelectedRow
. Для разделенной таблицы вам нужно перевести комбинацию разделов/строк в ссылку на данные.
Стандартный способ сделать это с массивом массивов (например, dictionaryItems:[[Dictionary]]
). Таким образом, вы можете получить массив элементов, используя раздел пути индекса во внешнем массиве и конкретный элемент, используя строку пути индекса в массиве, возвращаемую ссылкой раздела.
--- UPDATE с помощью методов, которые требуют изменения кода в DictionaryTableViewController
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Assume a single section after a search
return (searchController.active) ? 1 : sectionTitles.count
}
// Create a standard way to get a Dictionary from an index path
func itemForIndexPath(indexPath: NSIndexPath) -> Dictionary? {
var result: Dictionary? = nil
if searchController.active {
result = searchResults[indexPath.row]
} else {
let wordKey = sectionTitles[indexPath.section]
if let items = cockpitDict[wordKey] {
result = items[indexPath.row]
}
}
return result
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DictionaryTableViewCell
if let dictionary = itemForIndexPath(indexPath) {
cell.wordLabel.text = dictionary.word
cell.definitionSmallLabel.text = dictionary.definition
} else {
print("Cell error with path \(indexPath)")
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDictionaryDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! DictionaryDetailViewController
if let dictionary = itemForIndexPath(indexPath) {
destinationController.dictionary = dictionary
} else {
print("Segue error with path \(indexPath)")
}
searchController.active = false
}
}
}
Привет, вы знаете, где я могу найти учебное пособие или помочь в том, как это сделать? :( –
@Jade Теперь игнорируем github .... Я обновлю свой ответ с измененным кодом. –
HI извините за задержку, это отлично работает. Я буду следовать шаг за шагом, чтобы попытаться учиться в следующий раз, спасибо вы очень мне очень помогаете :) –