我有一个数组的问题,我正在使用一个返回不同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: " ", 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]
但我也有同样的错误
最佳答案
单击顶部单元格时需要打印第一个值,如
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(arraytitleN[indexPath.row])
}
在此处分配值
contenido = cell.contenidoN.text!
不保证它是index=0处的值,因为对所有可见单元格调用
cellForRowAt
,所以可以将它分配给任何其他行的值关于ios - 数组返回错误位置的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56246702/