Search code examples
iosswiftuitableviewswift3nsarray

Array returns values ​in wrong positions


I have a problem with an Array, I'm working with a WebService that returns different URLs, I save this information in an array and then display it in a UITableView, the problem is that in the first position of my array there must be a link that say for example: www.sitioWeb.com, but when I want to recover it in the method didSelectedRowAt sometimes the first position has the second of my array

I have already reviewed the information contained in my array and everything looks good, the information I recover from the WebService recovers well and without problems

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayTituloN.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! InicioTableViewCell
    cell.tituloN.text = arrayTituloN[indexPath.row]
    cell.links.isHidden = true

    //Limpiamos texto de la descripcion de la noticia
    let descripcionI = arrayExcerptN[indexPath.row]
    let betaDescripcion = descripcionI.replacingOccurrences(of: "<p>", with: "")
    let alphaDescripcion = betaDescripcion.replacingOccurrences(of: "</p>", with: "")
    let descripcion = alphaDescripcion.replacingOccurrences(of: "&nbsp;", with: " ")
    cell.descripcionN.text = descripcion

    //Limpiamos el texto que tiene los links pdf
    let linkI = arrayContentN[indexPath.row]
    let betaLink = linkI.components(separatedBy: ".pdf")
    let linkA: String = betaLink[0]
    let linkX = linkA.components(separatedBy: "https:")
    let linkS: String = linkX[1]

    let linkUltra: String = "https:"+linkS+".pdf"
    cell.contenidoN.text = linkUltra
    cell.contenidoN.isHidden = true

    print(cell.contenidoN.text!)

    //Mostraremos imagenes dependiendo el link
    if((cell.contenidoN.text?.contains(".pdf"))! && (cell.contenidoN.text?.contains(".png"))!){
        cell.imagenN.image = UIImage(named: "ImagenLauncher")
    }else if(cell.contenidoN.text?.contains(".pdf"))!{
        cell.imagenN.image = UIImage(named: "documentosXSize")
    }

    contenido = cell.contenidoN.text!

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(contenido)

}

When I press the first cell of my table, you should return something similar to this: "www.linkonthefirstcell.com" The problem is that sometimes I return the second value of my array in the first position.

I tried to assign my array to the variable "content" in the following way: var contenido: String = "" And when I load all the information with my WebService in the array I do the following:

contenido= arraytitleN[indexPath.row]

but I have the same error


Solution

  • You need to print first value when you click top cell like

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      print(arraytitleN[indexPath.row]) 
    }
    

    assigning the value here

    contenido = cell.contenidoN.text!
    

    doesn't guarantee it's the value at index = 0 , as cellForRowAt is called for all visible cells so it may be assigned to any value from any other row