2015-02-20 3 views
0

Я работаю с UILocalizedIndexedCollation и UISearchDisplayController, и мне нравится реализовать условие, которое применяется только при переопределении func, если пользователь не использует SerachBar.условное переопределение func - swift

Вот код, который переопределяет таблицу func для отображения Collation, но как я могу обойти этот код, если пользователь находится в панели поиска?

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    return self.collation.sectionForSectionIndexTitleAtIndex(index) 
} 

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String { 

    if !self.sections[section].users.isEmpty { 
     return self.collation.sectionTitles[section] as String 
    } 
    return "" 
} 

displayed to the right of the `UITableView` */ 
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject] { 
    return self.collation.sectionIndexTitles 
} 
+0

Какую функцию вы хотели бы условно условно отменить? –

ответ

0

Вы не собираетесь быть в состоянии условно переопределить метод, нормальный подход переопределить метод и вызвать супер, если вам не нужно переопределение:

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    if showingSearch { 
     return self.collation.sectionForSectionIndexTitleAtIndex(index) 
    } 
    else { 
     return super.tableView(tableView, sectionForSectionIndexTitle:title atIndex:Index) 
    } 
} 
+0

да, вы правы ... – Federico

0

finlally мой код выглядит так:

/* section headers appear above each `UITableView` section */ 
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String { 
    // do not display empty `Section`s 
    if self.sections[section].users.isEmpty || tableView == self.searchDisplayController!.searchResultsTableView { 
     return "" 
    } else { 
     return self.collation.sectionTitles[section] as String 
    } 
} 

/* section index titles displayed to the right of the `UITableView` */ 
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject] { 
    if tableView == self.searchDisplayController!.searchResultsTableView { 
     return tableView.visibleCells() 
    } else { 
     return self.collation.sectionIndexTitles 
    } 
} 
Смежные вопросы