Ios 我需要在这部分添加更多图像cell.topCellImg.image=UIImage(名为“image”)有什么可以帮我的吗

Ios 我需要在这部分添加更多图像cell.topCellImg.image=UIImage(名为“image”)有什么可以帮我的吗,ios,Ios,在图像资源中,创建新的图像集。然后将其命名为Image2。制作另一个图像集,命名为Image3,依此类推。然后将一些图像拖放到图像集中,然后使用以下代码 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TopCollectionViewCell cell.topCellTitleLabel.text = (topSubcategories[indexPat

在图像资源中,创建新的图像集。然后将其命名为Image2。制作另一个图像集,命名为Image3,依此类推。然后将一些图像拖放到图像集中,然后使用以下代码

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TopCollectionViewCell
cell.topCellTitleLabel.text = (topSubcategories[indexPath.row] as! String)
cell.topCellImg.image = UIImage(named:"Image")
return cell
  • var topSubcategories=Array() topSubcategories.append([“id”:“1”])

    0中的i的
    这是在给定数组的字典中添加图像键

  • 重新加载UITableView

  • 从单元格中动态更新imageView,就像对label所做的一样

  • 声明一个imageArray:

    for i in 0 ..< topSubcategories.count
    {
        var dict = topSubcategories[i]
        dict["image"] = // assign image here
        topSubcategories[i] = dict
    }
    

    要测试它,请使
    imageArray
    的计数与您在
    numberOfRows
    方法中返回的计数相同。

    对于初学者,请创建一个图像数组,如让imageArray=[“image1”、“image2”、“image3”]。然后cell.topCellImg.image=UIImage(名为:imageArray[indexPath.row])我使用了代码,但仍然面临相同的问题请编辑您的问题,并清楚地解释需求和问题。让cell=collectionView.dequeueReusableCell(withReuseIdentifier:“cell”,for:indexPath)为!TopCollectionViewCell cell.topCellTitleLabel.text=(topSubcategories[indexPath.row]as!String)cell.topCellImg.image=UIImage(名为:“图像”)我需要添加更多图像来代替(名为:“图像”我希望我正确理解了您试图实现的目标let cell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“cell”,用于:indexPath)as!TopCollectionViewCell.topCellTitleLabel.text=(topSubcategories[indexPath.row]as!String)imageArray=[UIImage(命名为:“Image”)!、UIImage(命名为:“Image2”)、UIImage(命名为:“Image3”)!]cell.topCellImg.Image=roomImage[indexPath.row]返回celllet cell=collectionView.DeQueueReuseAbleCell(使用ReuseIdentifier:“cell”,表示:indexPath)as!TopCollectionViewCell cell.topCellTitleLabel.text=(topSubcategories[indexPath.row]as!String)imageArray=[UIImage(命名为:“Image”)!、UIImage(命名为:“Image2”)、UIImage(命名为:“Image3”)!]cell.topCellImg.Image=roomImage[indexPath.row]返回cellhi,我用了这个,但没有得到我想要的required@user8604412此行将进入viewDidLoad。移动它:imageArray=[UIImage(名为:“Image1”)!、UIImage(名为:“Image2”)!、UIImage(名为:“Image3”)!]我遇到同样的问题…请让我知道确切的解决方案…你做了这么多?现在到底是什么问题?崩溃?哪一行?我已经创建了这个数组和我遵循的步骤,然后也遇到同样的问题…开始讨论这个问题。我现在只能给出一个图像:cell.topCellImg.image=UIImage(名为:“Emergency.png”)如果我自动创建图像数组,我的应用程序将崩溃
    for i in 0 ..< topSubcategories.count
    {
        var dict = topSubcategories[i]
        dict["image"] = // assign image here
        topSubcategories[i] = dict
    }
    
    var imageArray = [UIImage]()
    
    In viewDidLoad() :
    imageArray = [UIImage(named:"Image1")!,UIImage(named:"Image2")!,UIImage(named:"Image3")!]
    
    In cellForItem :
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TopCollectionViewCell
    
    //Also do optional binding using guard let/ if let
    if let titleText = topSubcategories[indexPath.row] as? String {
       cell.topCellTitleLabel.text = titleText
    }
    
    cell.topCellImg.image = imageArray[indexPath.row]
    return cell