【发布时间】:2019-08-14 09:14:12
【问题描述】:
我试图在表格视图单元格上显示随机图像,但是当我向下和向上滚动时,图像位置发生了变化。
我刚刚在单元格上添加了图像
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let imagesArr = ["img1","img2","img3","img4"]
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UITableViewDataSource,UITabBarDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let randomNumber = Int.random(in: 0...3)
let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell
cell.ImageView.image = UIImage(named: self.imagesArr[randomNumber])
return cell
}
}
我希望在向下和向上滚动之前获得图像
【问题讨论】:
-
所有图片的尺寸都一样吗?
-
是的,但这没关系。例如,如果第一个单元格上的图像是“img3”,当我向下和向上滚动时,我会看到它现在已更改为“img2”
-
如果您不希望他们更改随机播放列表并且不要在每次想要获取单元格时随机播放