Ios 在UITableView中滚动时重复图像

Ios 在UITableView中滚动时重复图像,ios,swift,uitableview,asynchronous,uiimageview,Ios,Swift,Uitableview,Asynchronous,Uiimageview,从下面的答案编辑代码 我想要什么 图像大小正确,显示位置正确 我得到的 当我滚动时,到处都会随机出现一堆图像 我看了这么多答案,但仍然没有找到解决问题的办法。对于每个人来说,这似乎是一个不同的问题,这里的大多数问题都是Objective-C,这并没有帮助。我发现很难从一个问题转换到另一个问题 我将展示我的表视图函数并解释我的代码,也许有人可以识别问题,或者帮助我自己识别并解决它 ---------------------------------------------------------

从下面的答案编辑代码

我想要什么

图像大小正确,显示位置正确

我得到的

当我滚动时,到处都会随机出现一堆图像

我看了这么多答案,但仍然没有找到解决问题的办法。对于每个人来说,这似乎是一个不同的问题,这里的大多数问题都是Objective-C,这并没有帮助。我发现很难从一个问题转换到另一个问题

我将展示我的表视图函数并解释我的代码,也许有人可以识别问题,或者帮助我自己识别并解决它

-----------------------------------------------------------------------------------------

解释

并非所有新闻项(单元格)都包含图片。因此,正如您所看到的(在下面的第一个函数中),我循环遍历各个部分和嵌套的单元格,如果新闻项包含图片
if newslists[I][j]!=“无
,我显示图像。从这一点上看,这是熟悉的代码,从教程中可以看到

在第二个函数中,我再次循环遍历各个部分和单元格,直到找到一个应该包含图像的单元格。然后,我更改该单元格的高度,使图像适合内部

差不多就是这样。。。 有什么想法吗

-----------------------------------------------------------------------------------------

CellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

    if newslists[indexPath.section][indexPath.row].imageURL != "None"{

        let urlString = newslists[indexPath.section][indexPath.row].imageURL

        let imgURL = NSURL(string: urlString)

        cell.imageView?.image = UIImage(named: "first")  // placeholder

        if let img = imageCache[urlString] {
            cell.imageView?.image = img
            println("loaded from cache")
        }
        else {

            let request: NSURLRequest = NSURLRequest(URL: imgURL!)
            let mainQueue = NSOperationQueue.mainQueue()
            NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
                if error == nil {

                    let image = UIImage(data: data)
                    self.imageCache[urlString] = image

                    dispatch_async(dispatch_get_main_queue(), {
                        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                            cellToUpdate.imageView?.image = image
                            println("succesfully downloaded image")
                            println("size of cache is \(self.imageCache.count)")
                        }
                    })
                }
                else {
                    println("Error: \(error.localizedDescription)")
                }

            })
        }
    }


    cell.backgroundColor = UIColor(red: 52/255, green: 138/255, blue: 169/255, alpha: 1.0)

    return cell

}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

if newslists[indexPath.section][indexPath.row].imageURL != "None"{
    println("Expanding cell for \(indexPath.section)  \(indexPath.row)")
    return 190.0
}

    return 70.0
}
我还有一个功能可能会导致问题,但我对此表示怀疑:

在索引路径上的行高度

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

    if newslists[indexPath.section][indexPath.row].imageURL != "None"{

        let urlString = newslists[indexPath.section][indexPath.row].imageURL

        let imgURL = NSURL(string: urlString)

        cell.imageView?.image = UIImage(named: "first")  // placeholder

        if let img = imageCache[urlString] {
            cell.imageView?.image = img
            println("loaded from cache")
        }
        else {

            let request: NSURLRequest = NSURLRequest(URL: imgURL!)
            let mainQueue = NSOperationQueue.mainQueue()
            NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
                if error == nil {

                    let image = UIImage(data: data)
                    self.imageCache[urlString] = image

                    dispatch_async(dispatch_get_main_queue(), {
                        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                            cellToUpdate.imageView?.image = image
                            println("succesfully downloaded image")
                            println("size of cache is \(self.imageCache.count)")
                        }
                    })
                }
                else {
                    println("Error: \(error.localizedDescription)")
                }

            })
        }
    }


    cell.backgroundColor = UIColor(red: 52/255, green: 138/255, blue: 169/255, alpha: 1.0)

    return cell

}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

if newslists[indexPath.section][indexPath.row].imageURL != "None"{
    println("Expanding cell for \(indexPath.section)  \(indexPath.row)")
    return 190.0
}

    return 70.0
}

我不知道为什么在这两种方法中都有一个双for循环。每个可能的indexPath都会被调用,这样您就可以通过indexPath节和行获得适当的数据

你可以试试这个代码,我看不出有什么理由它不起作用

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell
    cell.imageView?.image = nil

    if newslists[indexPath.section][indexPath.row].imageURL != "None"{

        let urlString = newslists[indexPath.section][indexPath.row].imageURL

        let imgURL = NSURL(string: urlString)

        cell.imageView?.image = UIImage(named: "first")  // placeholder

        if let img = imageCache[urlString] {
            cell.imageView?.image = img
            println("loaded from cache")
        }
        else {
            ...
        }
    }
    return cell
}



func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if newslists[indexPath.section][indexPath.row].imageURL != "None"{
        println("Expanding cell for \(i)  \(j)")
        return 190.0
    }

    return 70.0
}

多么尴尬,我简直不敢相信我添加了嵌套循环。虽然不幸,但仍然没有修复。你的问题到底是什么?我想我理解你的问题是什么,你应该重置图像视图,因为单元格正在被重用。我编辑了我的答案,并添加了
cell.imageView?.image=nil
我该怎么做?我已经添加了imagesBrilliant,修复了!我再感激不过了!