如何解决如何从资产中获取不同的图像并将其分配给不同表视图单元格中的图像视图
我正在尝试使用NFC阅读器会话将图像添加到我的tableview单元格中。因此,我的问题是,每次第一次阅读器会话时,我都会在图像视图中获得正确的图像,但是当我第二次尝试阅读器会话时,我在表视图的两个单元格上都停留了两个相同的最后分配的图像。我之所以知道它是因为tableView.dequeueReusableCell方法,但是我不确定使用哪种方法来获取正确的图像不正确的单元格。
我还附上了截图,以使我的意思更加清楚。
代码如下:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell",for: indexPath) as! TableViewCell
cell.nfcModel = arrData[indexPath.row]
// IMG CELL
cell.img.image = UIImage(named: name)
return cell
}
解决方法
不是NFC读者方面的专家。
1。创建一系列产品来存储NFC渲染中的产品数据。
2。在tableView func cellForRowAt
中,您可以从
使用displayMovieImage函数的favoriteMovies。
旁注:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var favoriteMovies: [Movie] = []
override func viewWillAppear(_ animated: Bool) {
mainTableView.reloadData()
super.viewWillAppear(animated)
if favoriteMovies.count == 0 {
favoriteMovies.append(Movie(id: "tt0372784",title: "Batman Begins",year: "2005",imageUrl: "https://images-na.ssl-images-amazon.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg"))
}
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let moviecell = tableView.dequeueReusableCell(withIdentifier: "customcell",for: indexPath) as! CustomTableViewCell
let idx: Int = indexPath.row
moviecell.tag = idx
//title
moviecell.movieTitle?.text = favoriteMovies[idx].title
//year
moviecell.movieYear?.text = favoriteMovies[idx].year
// image
displayMovieImage(idx,moviecell: moviecell)
return moviecell
}
func displayMovieImage(_ row: Int,moviecell: CustomTableViewCell) {
let url: String = (URL(string: favoriteMovies[row].imageUrl)?.absoluteString)!
URLSession.shared.dataTask(with: URL(string: url)!,completionHandler: { (data,response,error) -> Void in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async(execute: {
let image = UIImage(data: data!)
moviecell.movieImageView?.image = image
})
}).resume()
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。