按下tableview单元格时调用哪些函数?

时间:2017-09-15 17:50:32

标签: swift uitableview uiimageview cell uicollectionviewcell

cellForRowAt被调用时,我将每个单元格imageView' s设置为nil(这是为了清理重用的单元格保留其可回收的内容),然后我懒洋洋地从giphy获取gif& #39; s api。当来自giphy的api的gif返回时,我将单元格的图像设置为nil(否则gif将简单地重叠),然后是新到达的gif。这表现得如预期的那样;如果gif需要很长时间才能到达,我有占位符gif向用户发出工作正在进行的信号。当获取的gif最终到达时,它将替换占位符。

这是问题所在。当我按下一个单元格时,占位符会返回并重叠正确的gif。我无法弄清楚如何调试这个,因为当我按下一个单元格时,我不知道调用了什么函数。当我按下一个单元格时,不会调用didSelectRowAtcellForRowAt。只有在您释放触摸后才会调用didSelectRowAt

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let row = indexPath.row
    let cell = tableView.dequeueReusableCell(withIdentifier: giphCellReuseIdentifier, for: indexPath) as! GifTableViewCell
    print(#line, "<-Reached")
    cell.imageView?.image = nil
    cell.imageView?.loadGif(name: "giphyLogo")

    setCellAppearance(cell, row)

    cell.btn.addTarget(self, action: #selector(btnPress), for: .touchUpInside)
    cell.btn.tag = row

    cell.favoriteBtn.addTarget(self, action: #selector(alterFavCount), for: .touchUpInside)
    cell.favoriteBtn.tag = row

    return cell
    }


func setCellAppearance(_ cell: GifTableViewCell, _ row: Int) {
    isScrollingUp = row > lastCellForRowAtIndex
    lastCellForRowAtIndex = row


    if showOnlyFavorites {
        if let data = onlyFavoriteGifs[row].value(forKey: "data") as? Data {

            cell.img.downloadImageFrom(data)
        } else {
            cell.img.downloadImageFrom(cell, link: (onlyFavoriteGifs[row].value(forKey: "url") as? String)!, row, showOnlyFavorites)
        }
        cell.favoriteBtn.setImage(#imageLiteral(resourceName: "purpleHeartR"), for: .normal)
    } else {
        if let data = GifCache.shared.general.object(forKey: row as AnyObject) {
            print(#line, "<-Reached")
            cell.img.image = nil
            cell.img.downloadImageFrom(data as! Data)
        } else {
            print(#line, "<-Reached")
            cell.img.downloadImageFrom(cell, link: giphs[row].url, row, showOnlyFavorites)
        }

        if let wasSelected = buttonStates[giphs[row].url] {
            if wasSelected {
                cell.favoriteBtn.setImage(#imageLiteral(resourceName: "purpleHeartR"), for: .normal)
            } else {
                cell.favoriteBtn.setImage(#imageLiteral(resourceName: "WhiteHeartR"), for: .normal)
            }
        } else {
            cell.favoriteBtn.setImage(#imageLiteral(resourceName: "WhiteHeartR"), for: .normal)
        }
    }
}


extension UIImageView {
    func downloadImageFrom(_ cell: UITableViewCell, link:String, _ row: Int, _ favsOnly: Bool)  {
    URLSession.shared.dataTask( with: NSURL(string:link)! as URL, completionHandler: {
        (data, response, error) -> Void in
        if error != nil {
            print("error", error!, Date())
        }
        DispatchQueue.main.async {
            print(#line, "<-Reached")
            self.image = nil
            self.contentMode =  UIViewContentMode.scaleAspectFill
            if let data = data {
                let img = UIImage.gif(data: data)
                self.image = img

                DispatchQueue.global(qos: .background).async {
                    GifCache.shared.general.setObject(data as AnyObject, forKey: row as AnyObject)
                }
            }

        }
    }).resume()

}

func downloadImageFrom(_ data: Data) {
    print(#line, "<-Reached")
    self.image = nil
    let img = UIImage.gif(data: data)
    self.image = img

    }
}

0 个答案:

没有答案