Ios 滚动UITableView时图像消失
我正在使用Ios 滚动UITableView时图像消失,ios,swift,uitableview,Ios,Swift,Uitableview,我正在使用UITableView显示我的数据 我的数据由图像和文本组成。问题是当我滚动UITableView时,图像会消失 这是我的代码: func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return msg
UITableView
显示我的数据
我的数据由图像和文本组成。问题是当我滚动UITableView
时,图像会消失
这是我的代码:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return msgs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! collCell
let msg = msgs[indexPath.row]
var decodeImgURL = ""
cell.lbl1.text = msg.content
decodeImgURL = msg.imgurl
if decodeImgURL == "none" {
cell.img.isHidden = true
} else {
let dataDecoded : Data = Data(base64Encoded:decodeImgURL,options: .ignoreUnknownCharacters)!
let decodedImage = UIImage(data: dataDecoded)
cell.img.image = decodedImage
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let msg = msgs[indexPath.row]
if msg.imgurl == "none" {
return 64
} else {
return 207
}
}
使用
UITableView
,内存中只有可见的单元格。当您从屏幕上滚动表格单元格时,它将从内存中删除。这有助于加快表格视图的滚动。您面临的问题是,当您从屏幕上滚动表格单元格时,该单元格(连同图像)将从内存中释放。当您将单元格滚动回屏幕时,它将再次分配到内存中。我认为您的问题可能来自以下两个问题之一:
1) 您只需等待几秒钟,即可在单元格中重新创建图像
2) 您的
msgs
数组中可能存在问题。如果图像在几秒钟后没有出现,请尝试在这一行后面放置一个断点:cell.img.image=decodedImage
。如果在将单元格从屏幕上滚动到屏幕上后未达到该断点,则问题在于decodeImgURL
(图像的url)不正确。奇怪,因为对于Swift5
最简单的选择是每段时间(比如1秒)更新一次图像
然后使用
Timer.invalidate()
atviewdiddemouse()解除计时器的分配
method.进入lbl1
的msg.content
是否也消失了?您的图像是否会出现在第一个位置?@thedjnivek否lbl1仍然存在same@DamienBannerot是的,它出现在第一个位置我已经把断点放在cell.img.image=decodeImage
yaa上了当向下滚动,但它不会设置我的图像
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
self.pictureImageView?.image = your_image
}