【问题标题】:appending image array not appending image same image twice附加图像数组不附加图像两次相同的图像
【发布时间】:2020-12-31 04:03:13
【问题描述】:

我的 swift 代码目标是创建从 tableview 单元格附加的图像数组。但是,如果图像重复两次,则该代码有效。现在,当附加代码时,sc.jpg 在数组 somepics 中重复了两次。只打印一次 gwen.jpg、kids.jgp 和 sc.jpg。我希望 sc.jpg 在数组中出现两次,就像它在数组 somepics 中一样。

  var arrayThatStartsEmpty = [UIImage]()
  
  var somePics: [UIImage] = [UIImage(named: "gwen.jpg")!, UIImage(named: "kids.jpg")!, UIImage(named: "sc.jpg")!, UIImage(named: "sc.jpg")!]
     
  @objc func collect(){
    let collect = (0..<arr.count).compactMap { (theTable.cellForRow(at: IndexPath(row: $0, section: 0)) as? customtv)?.pic }
    let arrayValues2 = collect.compactMap { $0.image }
    arrayThatStartsEmpty.append(contentsOf: arrayValues2)
    print(arrayValues2)
  }

【问题讨论】:

  • arr 里面的 collect() 是什么?是`somePics`吗?

标签: arrays swift image append


【解决方案1】:

我对您的 collect() 方法进行了一些更改。你可以试试看

      @objc func collect(){
        let collect = arr.enumerated().compactMap { (index, _) in 
        (theTable.cellForRow(at: IndexPath(row: index, section: 0)) as? customtv)?.pic }
        let arrayValues2 = collect.compactMap { $0.image }
        arrayThatStartsEmpty.append(contentsOf: arrayValues2)
        print(arrayValues2)
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多