在Swift 4中无法显示标题(String)和图像从数组到CollectionView

我有一个包含不同图像URL的集合视图和数组。我想在收藏夹视图中显示标题和图像。但是我无法显示,也没有找到错误消息。 如何实现呢?在控制台中,所有结果都可以显示。我不知道该怎么做。

package require math::geometry

set lineseg1 [list $x1 $y1 $x2 $y2]
set lineseg2 [list $x3 $y3 $x4 $y4]
if {[math::geometry::linesegmentsIntersect $lineseg1 $lineseg2]} {
    set interPt [math::geometry::findLinesegmentIntersection $lineseg1 $lineseg2]
    puts "the line segments intersect at ($interPt)"
} else {
    puts "the line segments don't intersect"
}

}

这是控制台中的结果: [“ Vesuvio鸡肉”,“ Paprikash鸡肉”,“鸡肉汁”,“ Catalan鸡肉”,“波斯鸡肉”,“ Kreplach(鸡肉饺子)”,“ Dijon鸡肉”,“烤鸡肉”,“ Cacciatore鸡肉”,“龙蒿鸡“] [“ https://www.edamam.com/web-img/e42/e42f9119813e890af34c259785ae1cfb.jpg”,“ https://www.edamam.com/web-img/e12/e12b8c5581226d7639168f41d126f2ff.jpg”,“ https://www.edamam.com/web-img/fd1/fd1afed1849c44f5185720394e363b4e.jpg”,“ https://www.edamam.com/web-img/4d9/4d9084cbc170789caa9e997108b595de.jpg”,“ https://www.edamam.com/web-img/8f8/8f810dfe198fa3e520d291f3fcf62bbf.jpg”]

xiangmuwei 回答:在Swift 4中无法显示标题(String)和图像从数组到CollectionView

您正试图在无法使用的dataSource函数(collectionView:numberOfItemsInSection:)中设置collectionView的委托和数据源。

而是在您的viewController的viewDidLoad中设置委托和数据源,或者直接在界面构建器中使用情节提要。

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.delegate = self
    collectionView.dataSource = self

    //retrieve screen size
    fullScreenSize = UIScreen.main.bounds.size

    // setup backgroud color
    self.view.backgroundColor = UIColor.white

    fetchFoodList()
}

还要确保在fetchFoodList()的完成块中调用collectionView.reloadData()。

func fetchFoodList() {

   let url = URL(string: SomeUrlString)

   let task = URLSession.shared.dataTask(with: url!){ (data,response,error) in

   if error != nil{
       print(error!)
   } else {
        if let urlContent = data{
            do {
                let json = try JSON(data:data!)
                let recipes = json["hits"]
                self.foodTitle =  json["hits"].arrayValue.map {$0["recipe"]["label"].stringValue}
                print(self.foodTitle)

                var foodImage =  json["hits"].arrayValue.map {$0["recipe"]["image"].stringValue}

                print(foodImage)
                print(self.foodImage)

                DispatchQueue.main.async {
                   self.collectionView.reloadData()
                }
            }
            catch{
                print("JSON Processing Failed")
            }
        }
    }
    task.resume()
}
,

您必须设置collectionView的数据源并将其委托到不在(collectionView:numberOfItemsInSection:)

中的viewController的viewDidLoad中。
override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self
    //retrieve screen size
    fullScreenSize = UIScreen.main.bounds.size

    // setup backgroud color
    self.view.backgroundColor = UIColor.white

    fetchFoodList()
}
本文链接:https://www.f2er.com/3042330.html

大家都在问