uitableview swift detailtextlabel

UITableViewCell no muestra detailTextLabel.text-Swift



(7)

El texto de detalle (subtítulo) no aparece. Sin embargo, los datos están disponibles, porque cuando se agrega una llamada println (), imprime Opcional ("datos") en la consola con los datos esperados. En el guión gráfico, el UITableViewController se establece en la clase adecuada, el Estilo de celda de vista de tabla se establece en ''Subtítulo'' y el identificador de reutilización se establece en ''celda''. ¿Cómo puedo obtener la información de subtítulos para mostrar?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell dispatch_async(dispatch_get_main_queue(), { () -> Void in cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String println(self.myArray[indexPath.row]["subtitle"] as? String) // The expected data appear in the console, but not in the iOS simulator''s table view cell. }) return cell }


El mismo problema aquí (por lo que he leído, ¿quizás un error en iOS 8?), Así es como lo resolvimos:

  1. Elimina la célula prototipo de tu guión gráfico.

  2. Eliminar esta línea:

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

  1. Reemplazar con estas líneas de código:

let cellIdentifier = "Cell" var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier) }

Actualización para Swift 3.1, Xcode versión 8.3.3:

deja cellIdentifier = "Cell"

var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier) }


Para lo que vale: tuve el problema de que los detalles no aparecieran. Eso fue porque había registrado la celda tableView, que no debería haber hecho ya que el prototipo de celda se definió directamente en el guión gráfico.


Si aún desea utilizar celdas prototipo de su guión gráfico, seleccione el estilo TableViewcell como Subtítulo. funcionará.


Si está configurando el texto en cero en algún lugar cuando intenta establecerlo en un valor no nulo, la vista real que contiene el texto faltará. Esto fue introducido en iOS8. En su lugar, intente configurar un espacio vacío @" " carácter.

Vea esto: los subtítulos de UITableViewCell no se actualizarán


Si lo hace de manera programática sin celdas en el generador de interfaces, este código funciona como un encanto en Swift 2.0+

override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. yourTableView.delegate = self yourTableView.dataSource = self yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return yourTableViewArray.count } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell cell.textLabel?.text = "the text you want on main title" cell.detailTextLabel?.text = "the text you want on subtitle" return cell }


Su código se ve bien. Simplemente vaya al guión gráfico y seleccione la celda de su vista de tabla -> Ahora vaya a Inspector de atributos y elija el estilo a Subtítulo.

Siga esto de acuerdo con la captura de pantalla de abajo.

Espero que haya ayudado ..


Swift 4
foto de ejemplo tableview
Célula de TableView estándar con TextLabel, detailTextLabel y, opcionalmente, también imageView

Guión gráfico:
1. Haga clic en tableView y, en los atributos, elija una celda prototipo.
2. Haga clic en la celda que ahora apareció en el guión gráfico y en sus atributos establezca un estilo que incluya el lugar para el texto de detalle, por ejemplo, "Subtítulo"
3.En los atributos de las celdas establezca también un nombre de identificador para la celda.

ViewController:
1. Apunte a la celda que creó con el identificador y haga un bucle a través de algunas matrices para llenar celdas

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifierName", for: indexPath) let cellImage = nameOfYourImagesArray[indexPath.row] cell.imageView?.image = cellImage let cellName = nameOfYourNamesArray[indexPath.row] cell.textLabel?.text = cellName let cellDetailText = nameOfYourDetailsArray[indexPath.row] cell.detailTextLabel?.text = cellDetailText return cell }