我正在使用TableViewController并从Closure中的firebase获取图像。我确实在本地数据库中获取了图像的名称,并在获取时将其保存在数组中。但是当我使用indexPath.row访问图像数组时,所有内容都会崩溃(而不是我需要的2个图像,所有图像都会被替换,当我滚动tableView时它会不断增加)。请指导我如何获取单元格内的图像。提前致谢
var imageList = [UIImage]()
func getData()
{
Data = NSMutableArray()
Data = ModelManager.getInstance().getAllData(planID)
let count = Data.count
for i in 0...count-1{
let demoPlan = (Data.object(at: i) as! workoutPlan)
ImageDemo.append(demoPlan.Image)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell2:workoutplanCell = tableView.dequeueReusableCell(withIdentifier: "workoutplanCell") as! workoutplanCell
cell2.planExerciseNameLabel.text = plan.Name
if(plan.Image.contains("plank")){
cell2.timerButton.isHidden = false
}
else{
cell2.timerButton.isHidden = true
}
var new_imgList = [String]()
new_imgList.append(ImageDemo[indexPath.row])
new_imgList.append(ImageDemo[indexPath.row] + "1")
let storage = Storage.storage().reference()
for x in new_imgList
{
let storageRef = storage.child("images/\(new_imgList[x]).jpg")
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
if let error = error{
print(error.localizedDescription)
return
}
else{
self.imageList.append(UIImage(data: data!)!)
}
if x == new_imgList{
cell2.planExerciseImage.animationImages = self.imageList
cell2.planExerciseImage.animationDuration = 2
cell2.planExerciseImage.startAnimating()
cell2.planDescription.delegate = self
cell2.planDescription.text = plan.Description
cell2.planDescription.sizeToFit()
cell2.planDescription.textColor = UIColor.black
self.imageList.removeAll()
new_imgList.removeAll()
}
}
}
return cell2
}
你可能有这个问题有两个原因:一个是imageList
范围。你已经在ViewController
的某处定义了它,并且每次设置一个单元格时它都会不断添加图像。
与第一个有点联系的第二个原因是if x == 1
我看不到这句话一直被调用,因此你的imageList
不会被清除。
从你声明的地方删除你的new_imgList
,而不是在这里声明:
for x in new_imgList{
// DECLARE new_imgList HERE
.....
}
并删除self.imageList.removeAll()
行。