uitableviewcell source number example data iphone ipad uitableview uistoryboard iboutlet

iphone - source - uitableviewcell swift 3



¿Cómo configurar el título de la sección UITableView mediante programación(iPhone/iPad)? (8)

No hay nada de malo con las otras respuestas, pero esta ofrece una solución no programática que puede ser útil en situaciones donde uno tiene una pequeña tabla estática. El beneficio es que uno puede organizar las localizaciones usando el guión gráfico. Uno puede continuar exportando localizaciones desde Xcode a través de archivos XLIFF. Xcode 9 también tiene varias herramientas nuevas para facilitar las localizaciones .

(original)

Tenía un requisito similar. Tenía una tabla estática con células estáticas en mi Main.storyboard (Base). Para localizar títulos de sección utilizando archivos .string, por ejemplo, Main.strings (alemán), simplemente seleccione la sección en el guión gráfico y tenga en cuenta la ID del objeto

Luego vaya a su archivo de cadena, en mi caso Main.strings (alemán) e inserte la traducción como:

"MLo-jM-tSN.headerTitle" = "Localized section title";

Recursos adicionales:

UITableView una UITableView en Interface Builder usando storyboards . La UITableView está configurada con static cells y varias secciones diferentes.

El problema que estoy teniendo es que estoy tratando de configurar mi aplicación en varios idiomas diferentes. Para hacer esto, necesito poder cambiar los títulos de la sección UITableView alguna manera.

Por favor alguien puede ayudarme? Idealmente, me gustaría abordar el problema utilizando IBOutlets sin embargo, sospecho que esto ni siquiera es posible en este caso. Cualquier consejo y sugerencia sería muy apreciada.

Gracias por adelantado.


No sé sobre las versiones anteriores de los protocolos UITableView , pero a partir de iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? es parte del protocolo UITableViewDataSource .

class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } } extension ViewController: UITableViewDataSource { func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "Section name" } }

No necesita declarar al delegate para llenar su tabla con datos.


Si está escribiendo código en Swift, se vería como un ejemplo como este

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { switch section { case 0: return "Apple Devices" case 1: return "Samsung Devices" default: return "Other Devices" } }


Tenga en cuenta que -(NSString *)tableView: titleForHeaderInSection: no lo llama si - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section se implementa en el delegado de UITableView;


Una vez que haya conectado su delegate UITableView y la fuente de datasource a su controlador, podría hacer algo como esto:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *sectionName; switch (section) { case 0: sectionName = NSLocalizedString(@"mySectionName", @"mySectionName"); break; case 1: sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName"); break; // ... default: sectionName = @""; break; } return sectionName; }


Use el método UITableViewDataSource

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section


titleForHeaderInSection es un método de delegado de UITableView para aplicar el texto del encabezado de la escritura de la sección de la siguiente manera:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"Hello World"; }


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 45.0f; //set height according to row or section , whatever you want to do! }

el texto de la etiqueta de sección está establecido.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionHeaderView; sectionHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, tableView.frame.size.width, 120.0)]; sectionHeaderView.backgroundColor = kColor(61, 201, 247); UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(sectionHeaderView.frame.origin.x,sectionHeaderView.frame.origin.y - 44, sectionHeaderView.frame.size.width, sectionHeaderView.frame.size.height)]; headerLabel.backgroundColor = [UIColor clearColor]; [headerLabel setTextColor:kColor(255, 255, 255)]; headerLabel.textAlignment = NSTextAlignmentCenter; [headerLabel setFont:kFont(20)]; [sectionHeaderView addSubview:headerLabel]; switch (section) { case 0: headerLabel.text = @"Section 1"; return sectionHeaderView; break; case 1: headerLabel.text = @"Section 2"; return sectionHeaderView; break; case 2: headerLabel.text = @"Section 3"; return sectionHeaderView; break; default: break; } return sectionHeaderView; }