Ios 如何从资源中获取不同的图像并将其分配到不同表视图单元格中的图像视图
我正在尝试使用NFC阅读器会话将图像添加到我的tableview单元格中。所以,我这里的问题是,每次读卡器会话,我都会在图像视图中得到正确的图像,但是当我第二次尝试读卡器会话时,我的表视图的两个单元格上都会出现两个相同的最后分配的图像。我知道这是因为tableView.dequeueReusableCell方法,但我不确定使用哪种方法来获得正确的图像和不正确的单元格 我还附上了一个截图,以使我的意思更清楚 代码如下:Ios 如何从资源中获取不同的图像并将其分配到不同表视图单元格中的图像视图,ios,swift,xcode,uitableview,nfc,Ios,Swift,Xcode,Uitableview,Nfc,我正在尝试使用NFC阅读器会话将图像添加到我的tableview单元格中。所以,我这里的问题是,每次读卡器会话,我都会在图像视图中得到正确的图像,但是当我第二次尝试读卡器会话时,我的表视图的两个单元格上都会出现两个相同的最后分配的图像。我知道这是因为tableView.dequeueReusableCell方法,但我不确定使用哪种方法来获得正确的图像和不正确的单元格 我还附上了一个截图,以使我的意思更清楚 代码如下: func tableView(_ tableView: UITableVi
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 render中的产品数据 2.在tableView
func cellForRowAt
中,可以从
使用displayMovieImage功能查看收藏夹。
旁注:
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()
}
很抱歉,我不能帮助解决NFC问题,我在tableView和如何显示Web API中的数据方面做了很多工作,希望代码示例能有所帮助