TransWikia.com

¿Comó expandir tableview cell?

Stack Overflow en español Asked by Carlos Padron on November 23, 2020

Tengo un tableview con celdas que se extienden de acuerdo con el contenido de la celda. Lo que trato de hacer es cuando se cargue la informacion en CellForRowAt IndexPath tenga el tamaño de la altura del label que está en la celda más las constraints para que al momento de seleccionar la celda en didSelectRowAt indexPath pueda expandir la celda al tamaño total para que se pueda ver todo su contenido.

Espero poder ocultar el texto para que cuando hagan clic en la celda, se haga lo de expandir

Espero poder ocultar el texto para que cuando hagan clic en la celda, se haga lo de expandir

Lo que he tratado de hacer es que HeightFotRowAt IndexPath es obtener la celda acutal y las medidas que comenté en el parrafo anterior pero lo anterior regresa un menasje

[Assert] Attempted to call -cellForRowAtIndexPath: on the table view
while it was in the process of updating its visible cells, which is
not allowed. Make a symbolic breakpoint at
UITableViewAlertForCellForRowAtIndexPathAccessDuringUpdate to catch
this in the debugger and see what caused this to occur. Perhaps you
are trying to ask the table view for a cell from inside a table view
callback about a specific row? Table view

De acuerdo con lo que ví cuando en cellForForAt IndexPath cuando llega a la parte de dequeueReusableCell se va al método HeightFotRowAt IndexPath y no entra donde obtengo la info de la celda, despuès regresa a cellForForAt IndexPath y termina de ejecutarse.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView === MainTable{
            if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
                
                let backgroundView              =  UIView()
                backgroundView.backgroundColor  =  UIColor.white.withAlphaComponent(0.0)
                cell.selectedBackgroundView     =  backgroundView
                
                let index                       =  indexPath.row
                cell.setMedName(name: self.medCatalog[index].nombre,
                                uso: self.medCatalog[index].uso )
                
                
                return cell
            }
        }
        return UITableViewCell()
    }



func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if let cell =  tableView.cellForRow(at: indexPath) as? MedCell {

        let nameHeight             =  cell.medText.bounds.height
        let topAnchor: CGFloat     =  12.0
        let bottomAnchor: CGFloat  =  10.0

        let cellHeight             =  nameHeight + topAnchor + bottomAnchor
        print(cellHeight)

        return cellHeight
        
    }
    return 85
}

One Answer

El warning es muy claro, no puedes acceder al row que recién se está dibujando en esta línea en el método heightForRowAt:

if let cell =  tableView.cellForRow(at: indexPath) as? MedCell {

Puedes intentar esto: Declara estos diccionarios en donde vas a almacenar un bool que te diga si es la celda que necesitas y en la otra el valor del bounds del text de la celda

var cellsH: [Int:CGFloat] = [:]
var cellB: [Int:Bool] = [:]

Cuando creas la celda agregas los valores a esos diccionarios:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView === MainTable{
            if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
                
                let backgroundView              =  UIView()
                backgroundView.backgroundColor  =  UIColor.white.withAlphaComponent(0.0)
                cell.selectedBackgroundView     =  backgroundView
                
                let index                       =  indexPath.row
                cell.setMedName(name: self.medCatalog[index].nombre,
                                uso: self.medCatalog[index].uso )
                
                //Aquí agregas los valores
                self.cellB[indexPath.row] = true
                self.cellsH[indexPath.row] = cell.medText.bounds.height

                return cell
            }
        }
        self.cellB[indexPath.row] = false
        self.cellsH[indexPath.row] = 0.0
        return UITableViewCell()
    }

Ahora al momento de modificar el height trabajas con los valores de esos diccionarios en la posición del indexPath.row:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if self.cellB[indexPath.row] != nil {
        if self.cellB[indexPath.row]! {
            let nameHeight             =  self.cellH[indexPath.row]!
            let topAnchor: CGFloat     =  12.0
            let bottomAnchor: CGFloat  =  10.0

            let cellHeight             =  nameHeight + topAnchor + bottomAnchor
            print(cellHeight)
            return cellHeight
        }
    }
        
    return 85
}

Correct answer by Bicho on November 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP