ios swift tableview uitableviewsectionheader

ios - ¿Cómo cambia el color de un título de sección en una vista de tabla?



swift tableview (5)

Puede hacer su propia vista de título de sección (encabezado / pie de página), y es fácil.

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 } }

Esto es lo que tengo en este momento.

¿Cómo me refiero a esto para poder cambiar el color del texto para que coincida con mi lista de índice? La sección ForSectionIndexTitle funcionó bien para agregar el título de la sección correcta, pero ¿cómo se accede exactamente al elemento del título?

¿O es imposible y necesito volver a dibujar la vista y agregarla con viewForHeaderInSection ?


Título Personalizado:

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() }


Una solución de revestimiento (mediante encadenamiento opcional):

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


Yo usaría la clase proxy Apariencia (). Usualmente los agrego en una función en AppDelegate y los llamo didFinishLaunching.

private func setupApperances() { UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .red }


Puedes usar el método de UITableViewDelegate .

swift3 y superior

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { if let headerView = view as? UITableViewHeaderFooterView { headerView.contentView.backgroundColor = .white headerView.textLabel?.textColor = .red } }

C objetivo

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

Para referencia tomé la respuesta del modelo desde here