【问题标题】:Delete previous item tableview Swift删除上一项 tableview Swift
【发布时间】:2015-09-04 12:34:22
【问题描述】:

我的一个 tableview 遇到问题,我解码 JSON 并将结果放入我的单元格,问题是,每次我关闭视图并返回它时,以前的项目仍然在这里:

我想我和 ReloadData 有关系,但我不知道是什么。

我的代码:

//Retrieving JSON DATA : 

var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
            var DataFromJSon = jsonResult["objects"] as! NSArray
            for one in DataFromJSon {
                let repo = Repository(jsonData: one as! [String : AnyObject])
                repos.append(repo)

            }

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

    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
    if (cell != nil)
    {   
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier: "Cell")
    }
    cell!.textLabel?.text =  repos[indexPath.row].title
    cell!.detailTextLabel?.text = repos[indexPath.row].price


    //on récupère l'url de l'image dans "Repository" et on la convertis en data exploitable
    let urlString: NSString = repos[indexPath.row].picture_url! ,
    imgURL = NSURL(string: urlString as String)

    if let url = NSURL(string: urlString as String) {
        // If this image is already cached, don't re-download
        if let img = imageCache[urlString as String] {
            cell!.imageView?.image = img
            cell!.imageView?.frame = CGRectMake(5,5,35,35)
            println("image présente")
        }
        else {
            // The image isn't cached, download the img data
            // We should perform this in a background thread
            let request: NSURLRequest = NSURLRequest(URL: imgURL!)
            let mainQueue = NSOperationQueue.mainQueue()
            NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
                if error == nil {
                    // Convert the downloaded data in to a UIImage object
                    let image = UIImage(data: data)
                    // Store the image in to our cache
                    self.imageCache[urlString as String] = image
                    // Update the cell

                    dispatch_async(dispatch_get_main_queue(), {
                        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                            cellToUpdate.imageView?.image = image
                            cellToUpdate.imageView!.frame = CGRectMake(5,5,35,35)
                            println("image reload")
                        }
                    })
                }
                else {
                    println("Error: \(error.localizedDescription)")
                }
            })
        }


    }
    return cell!
}

我该如何解决这个问题?

谢谢!

【问题讨论】:

  • 好像你追加新下载的数据,而不是重写旧数据。
  • 也许你需要我的部分代码?
  • 它会澄清一些事情,是的。
  • 看我更新的帖子!

标签: swift tableview reloaddata


【解决方案1】:

在将新项目附加到数组之前清除以前的 repos。

var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
var DataFromJSon = jsonResult["objects"] as! NSArray

repos.removeAll() // clear old items.[Add this line of code]

for one in DataFromJSon {
    let repo = Repository(jsonData: one as! [String : AnyObject])
    repos.append(repo)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多