2016-09-21 11 views
10

Oto, co mam w tej chwili.Jak zmienić kolor tytułu sekcji w widoku tabeli?

enter image description here

jaki sposób odnoszą się do tego, aby można było zmienić kolor tekstu pasujące do mojej listy indeksu? Sekcja sectionForSectionIndexTitle działała dobrze, dodając prawidłowy tytuł sekcji, ale jak dokładnie uzyskuje dostęp do elementu tytułu?

Czy jest to niemożliwe i muszę przerysować widok i dodać go za pomocą viewForHeaderInSection?

+0

Tak, musisz dodać etykietę w viewForHeaderInSection. – Janmenjaya

+0

Hye man, jak dodać alfabet po prawej? –

+0

To się nazywa lista indeksów. Oto vid https://www.youtube.com/watch?v=xYSKHna1KJk. Lub inny link z doskonałym samouczkiem. http://www.edumobile.org/ios/indexed-table-views-in-swift/ – AMAN77

Odpowiedz

26

można użyć jednego z UITableViewDelegate „s metoda

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) { 
    if let headerTitle = view as? UITableViewHeaderFooterView { 
     headerTitle.textLabel?.textColor = UIColor.redColor() 
    } 
} 

swift3 a przede

func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) { 
    if let headerTitle = view as? UITableViewHeaderFooterView { 
     headerTitle.textLabel?.textColor = UIColor.red 
    } 
} 

obiektywnej C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){ 
     UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; 
     tableViewHeaderFooterView.textLabel.textColor = [UIColor RedColor]; 
    } 
} 

porównawczy wziąłem model odpowiedzi od here

+0

Dziękujemy, działa świetnie! – AMAN77

+0

Prawdopodobnie moja ulubiona odpowiedź na SO. –

1

Własny tytuł:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let title = UILabel() 
    title.font = UIFont(name: "SFUIDisplay-Light", size: 13)! 
    title.textColor = UIColor.redColor() 

    let header = view as! UITableViewHeaderFooterView 
    header.textLabel!.font=title.font 
    header.textLabel!.textColor=title.textColor 
    header.contentView.backgroundColor = UIColor.whiteColor() 
} 
2

jedno rozwiązanie liniowej (przy użyciu opcjonalnego łańcuchowym):

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    (view as? UITableViewHeaderFooterView)?.textLabel?.textColor = UIColor.red 
} 
+0

Czym to się różni od rozwiązania @ Anbu.karthik? –

+0

^^ Rezultat jest taki sam, ale ten używa opcjonalnego łańcucha, co daje jedną linię, zamiast warunku 'if let'. Zaktualizuję odpowiedź, wskazując na to –

0

Można zrobić własny tytuł sekcji (nagłówek/stopka) Zobacz, i to jest łatwe.

class BlackTableViewHeaderFooterView : UITableViewHeaderFooterView { 

    override init(reuseIdentifier: String?) { 
     super.init(reuseIdentifier: reuseIdentifier) 

     contentView.backgroundColor = .black 

     textLabel?.font = UIFont.preferredFont(forTextStyle: .body) 
     textLabel?.numberOfLines = 0 
     textLabel?.textColor = .white 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

class TableViewController : UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.register(BlackTableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "\(BlackTableViewHeaderFooterView.self)") 
     // do other setup 
    } 

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(BlackTableViewHeaderFooterView.self)") 
     header.textLabel?.text = "" // set your header title 
     return header 
    } 
} 
Powiązane problemy