如何从资产中获取不同的图像并将其分配给不同表格视图单元格中的图像视图

Posted

技术标签:

【中文标题】如何从资产中获取不同的图像并将其分配给不同表格视图单元格中的图像视图【英文标题】:How to get different image from assets and assign it to image view in different table view cells 【发布时间】:2020-09-08 02:50:16 【问题描述】:

我正在尝试使用 NFC 阅读器会话将图像添加到我的 tableview 单元格。所以,我的问题是,每次第一次阅读器会话,我都会在图像视图中获得正确的图像,但是当我第二次尝试阅读器会话时,我在表格视图的两个单元格上都遇到了两个相同的最后分配的图像。我知道它是因为 tableView.dequeueReusableCell 方法,但我不确定使用哪种方法来获取正确的图像不正确的单元格。

我还附上了一张截图,以更清楚地说明我的意思。

In the screenshot is should see an image of a water bottle from my assets, but instead, I am getting the last assigned image to every cell

代码如下:

 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 问题,我在 tableView 以及如何显示来自 Web API 的数据方面做了很多工作 希望代码示例有所帮助 【参考方案1】:

不是 NFC 读取器方面的专家。

1.创建一个产品数组来存储来自 NFC 渲染的产品数据。

2.in 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()
        

【讨论】:

以上是关于如何从资产中获取不同的图像并将其分配给不同表格视图单元格中的图像视图的主要内容,如果未能解决你的问题,请参考以下文章

如何分解数组并将块分配给各个复选框字段?

我在自定义表格视图中有 FB 朋友的图像和名称。如何获取 FB ID 中的选定行并将请求发送给多个朋友?

从网络加载图像并将其显示在集合视图中

从表格视图单元格内的文本字段中获取文本

从资产中获取实际图像名称

如何检索操作栏的标题并将其分配给文本视图?