简体   繁体   English

直到选择了单元格,URL中的Swift tableview图像才会显示

[英]Swift tableview image from URL not showing until cell is selected

I'm iterating through an array of URLs in order to display images in my tableview cells. 我正在遍历URL数组,以便在表格单元格中显示图像。 For some reason the images will not display until the cell is selected. 由于某些原因,只有选中该单元格,图像才会显示。 Only one image will show at a time, so when I select on one cell to display the image, it will disappear again once I select another cell. 一次只能显示一张图像,因此当我选择一个单元格以显示图像时,一旦选择另一个单元格,它将再次消失。 I'm not sure what the issue is because the text shows up with no issues: 我不确定是什么问题,因为文本显示没有问题:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ResultsTableViewCell
   var place = [String]()
    var img = [String]()

    if imageUrls.count == venueIds.count {
        for (key,value) in restaurantInfo {
            place.append(value[0])
            img.append(value[1])
        }
            let search = img[indexPath.row]
            Alamofire.request(search).responseImage { response in
                if let image = response.value {
                    cell.imageSelection.image = image
                }
                else{
                    print(response.result.error!)
                }
        }
        cell.newLabel.text = place[indexPath.row]
    }
    return cell
}

Hey you should not call web service from the cellForRowAt method ideally, instead prepare a model for your table and fetch the data beforehand. 嘿,理想情况下,您不应从cellForRowAt方法调用Web服务,而应为表准备一个模型并事先获取数据。

Or 要么

Also image fetching can be done asynchronously, so that your table's scroll don't get laggy and images display correctly. 同样,图像获取也可以异步完成,这样表的滚动不会变得迟钝并且图像可以正确显示。

DispatchQueue.main.async {}

Instead of trying to add the image to the cell, add the image to your tableView as the cell.backgroundImage . 与其尝试将图像添加到单元格中,不如将图像作为cell.backgroundImage添加到tableView中。 Hope that solves your problem! 希望能解决您的问题!

It's hard to say without seeing anything in your custom cell or your didSelectRow method, but my guess is that this is happening because cellForRowAt is being called on each of your cells before your Alamofire requests finish. 很难说没有在自定义单元格或didSelectRow方法中看到任何内容,但我的猜测是这种情况正在发生,因为在Alamofire请求完成之前,将在每个单元格上调用cellForRowAt

tableView(_:cellForRowAt:) is called very often - each time a cell is about to be dequeued and come onscreen. tableView(_:cellForRowAt:)经常被调用-每次要将一个单元从队列中取出并显示在屏幕上时。 I'd advise against putting a network call in here as Mansi mentioned. 我建议不要像曼西所说的那样在这里打网络电话。 You also probably don't want to be doing that loop to get restaurant info in this function either. 您也可能不想在该函数中执行该循环来获取餐厅信息。 Both the loop and the service call will be called for each cell, every time that cell is about to be dequeued and come onscreen - this is going to get very expensive very fast. 每当该单元将要出队并出现在屏幕上时,将为每个单元调用循环和服务调用-这将变得非常昂贵。 It also looks by this code that when one of the table view's cells goes offscreen then is about to come onscreen again, that cell will attempt to re-download the image even if already was downloaded previously (because cellForRow will get call again for that cell). 通过此代码还可以看到,当表格视图的某个单元格脱离屏幕然后即将再次出现在屏幕上时,即使先前已经下载了该单元格,它也会尝试重新下载图像(因为cellForRow将再次对该单元格进行调用)。

The text label is showing up without issues because it doesn't rely on any network call and can be drawn right away. 文本标签显示没有问题,因为它不依赖于任何网络调用,并且可以立即绘制。 My guess is your didSelectRow or custom cell is telling the cell to re-draw itself when selected, which is why that image is showing up when you select it. 我的猜测是您的didSelectRow或自定义单元格告诉该单元格在被选中时会重新绘制自身,这就是为什么当您选择该图像时会显示该图像的原因。

I'd suggest moving the loop and the request outside of this function and creating some sort of data structures to hold these values. 我建议将循环和请求移到该函数之外,并创建某种数据结构来保存这些值。 You might need to implement a loading animation in your image views while waiting for the requests to complete, and a data structure for your images could act as a cache. 在等待请求完成时,您可能需要在图像视图中实现加载动画,并且图像的数据结构可以充当缓存。 There's a few different ways to go about this - these are just general suggestions. 有几种不同的解决方法-这些只是一般性建议。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM