【问题标题】:Array returns values ​in wrong positions数组在错误的位置返回值
【发布时间】:2019-10-08 08:16:28
【问题描述】:

我的数组有问题,我正在使用返回不同 URL 的 WebService,我将此信息保存在数组中,然后将其显示在 UITableView 中,问题是在我的数组的第一个位置必须是一个链接,例如:www.sitioWeb.com,但是当我想在方法 didSelectedRowAt 中恢复它时,有时第一个位置是我数组的第二个

我已经查看了我的数组中包含的信息,一切看起来都很好,我从 WebService 恢复的信息恢复得很好并且没有问题

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)

}

当我按下表格的第一个单元格时,您应该返回类似于以下内容的内容:“www.linkonthefirstcell.com” 问题是有时我会在第一个位置返回数组的第二个值。

我尝试通过以下方式将我的数组分配给变量“content”:var contenido: String = "" 当我使用数组中的 WebService 加载所有信息时,我会执行以下操作:

contenido= arraytitleN[indexPath.row]

但我有同样的错误

【问题讨论】:

    标签: ios swift uitableview swift3 nsarray


    【解决方案1】:

    单击顶部单元格时需要打印第一个值

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

    在这里赋值

    contenido = cell.contenidoN.text!
    

    不保证它是 index = 0 处的值,因为所有可见单元格都会调用 cellForRowAt,因此它可以分配给任何其他行中的任何值

    【讨论】:

    • 当我在方法 didSelectedRowAt 中打印第一个值时,如果我正确打印了单元格的值,问题是如果我想使用包含每个单元格的链接,有时我会在其中加载第二个值第一个值
    • 可能是评论含糊不清,但是您是否还有其他导致该问题的上下文?还是上面的代码会发生这种情况? didSelectRowAt 将显示您的数据源中单击的单元格的内容
    • 很抱歉我没有很好地理解你的答案,但你是对的,我所做的是打印我的单元格的值并查看它们是否正确并将值分配给我的变量并通过 segue 发送,谢谢!
    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多