here is my code it all parsit everything works but for some reason it creates too many copies

class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var NameLabel: UILabel! @IBOutlet weak var LogoPicture: UIImageView! let zoomImageView = UIView() let urlString = "sdfsdf" @IBOutlet weak var tableView: UITableView! var nameArray = [String]() var timeArray = [NSNumber]() var logoArray = [String]() var bodyArray = [String]() var img1Array = [String]() var img2Array = [String]() var img3Array = [String]() var titleArray = [String]() override func viewDidLoad() { super.viewDidLoad() /*zoomImageView.frame = CGRect (x: 32, y: 0, width: 200, height: 100) zoomImageView.backgroundColor = UIColor.red zoomImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "animate")) view.addSubview(zoomImageView)*/ let defaults = UserDefaults.standard if let name = defaults.string(forKey: "NameText"){ NameLabel.text = name } if let logo = defaults.string(forKey: "LogoText"){ let strurl = NSURL(string: logo) if let dtinternet = NSData(contentsOf:strurl! as URL) { LogoPicture.image = UIImage(data: dtinternet as Data) } self.view.addSubview(LogoPicture) LogoPicture.layer.borderWidth = 1 LogoPicture.layer.masksToBounds = false LogoPicture.layer.borderColor = UIColor.clear.cgColor LogoPicture.layer.cornerRadius = LogoPicture.frame.height/2 LogoPicture.clipsToBounds = true} self.downloadJsonWithURL() } @IBAction func ExitButton(_ sender: Any) { UserDefaults.standard.set(false, forKey: "isUserLoggedIn"); UserDefaults.standard.synchronize(); } func downloadJsonWithURL(){ let url = NSURL(string: urlString) URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { //print(jsonObj!.value(forKey: "data")) if let dataArray = jsonObj!.value(forKey: "data") as? NSArray { for datata in dataArray { if let dataDict = datata as? NSDictionary { if let title = dataDict.value(forKey: "title") { self.titleArray.append(title as! String) } if let created = dataDict.value(forKey: "created") { created as! Double let date = NSDate(timeIntervalSince1970: TimeInterval(created as! NSNumber)) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" self.timeArray.append(dateFormatter.string(from: date as Date)) } if let body = dataDict.value(forKey: "body") { self.bodyArray.append(body as! String) } if let img1 = dataDict.value(forKey: "image1") { self.img1Array.append(img1 as! String) } else { self.img1Array.append(" nil " as! String)} if let img2 = dataDict.value(forKey: "image2") { self.img2Array.append(img2 as! String) } else { self.img2Array.append(" nil " as! String)} if let img3 = dataDict.value(forKey: "image3") { self.img3Array.append(img3 as! String) } else { self.img3Array.append(" nil " as! String)} if let authorArray = dataArray.value(forKey: "author") as? NSArray { for datata1 in authorArray { if let authorDict = datata1 as? NSDictionary { if let namea = authorDict.value(forKey: "name") { self.nameArray.append(namea as! String) } if let logo = authorDict.value(forKey: "logo") { self.logoArray.append(logo as! String) } } } } OperationQueue.main.addOperation({ self.tableView.reloadData()}) } //return } } //print(self.timeArray) } }).resume() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return timeArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell cell.nameLable.text = "\(nameArray[indexPath.row])" cell.likeLabel.text = bodyArray[indexPath.row] //print(nameArray[indexPath.row]) cell.dataLabel.text = timeArray[indexPath.row] /*let imgURL = NSURL(string: logoArray[indexPath.row]) if imgURL != nil { let data = NSData(contentsOf: imgURL! as URL) cell.logoImg.image = UIImage(data: data! as Data) }*/ let imageView = cell.logoImg! imageView.sd_setImage(with: URL(string: logoArray[indexPath.row])) let imgURL3 = NSURL(string: img3Array[indexPath.row]) let imgURL2 = NSURL(string: img2Array[indexPath.row]) let imgURL1 = NSURL(string: img1Array[indexPath.row]) cell.img1.frame = CGRect(x: 13, y: 5, width: 300, height: 145) cell.img1.isUserInteractionEnabled = true //cell.img1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "animate")) //cell.img2.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "animate")) //cell.img3.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "animate")) if imgURL3 == nil { cell.img3.image = nil if imgURL2 == nil { cell.img2.image = nil if imgURL1 == nil { cell.img1.image = nil } else { cell.img1.frame = CGRect(x: 13, y: 5, width: 300, height: 145) let imageView1 = cell.img1! imageView1.sd_setImage(with: URL(string: img1Array[indexPath.row])) } } else { cell.img1.frame = CGRect(x: 13, y: 5, width: 147, height: 145) cell.img2.frame = CGRect(x: 166, y: 5, width: 147, height: 145) let imageView1 = cell.img1! imageView1.sd_setImage(with: URL(string: img1Array[indexPath.row])) let imageView2 = cell.img2! imageView2.sd_setImage(with: URL(string: img2Array[indexPath.row])) } } else { cell.img1.frame = CGRect(x: 13, y: 5, width: 147, height: 145) cell.img2.frame = CGRect(x: 166, y: 5, width: 144, height: 71) cell.img3.frame = CGRect(x: 166, y: 79, width: 144, height: 71) let imageView1 = cell.img1! imageView1.sd_setImage(with: URL(string: img1Array[indexPath.row])) let imageView2 = cell.img2! imageView2.sd_setImage(with: URL(string: img2Array[indexPath.row])) let imageView3 = cell.img3! imageView3.sd_setImage(with: URL(string: img3Array[indexPath.row])) } print(timeArray) return cell } func animate() { UIView.animate(withDuration: 0.75) { let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell let height = (cell.frame.width/200) * 100 let y = cell.frame.height / 2 - height / 2 cell.img1.frame = CGRect(x: 0, y: y, width: self.view.frame.width , height: height) } } } 

that's what it gives out through print(timeArray)

 [1497856315, 1497688102, 1497614594, 1497337111, 1493924259, 1492888300, 1492194473, 1492099489, 1491910949, 1491725464, 1491467821, 1491228362, 1490703933, 1490360502, 1490277769, 1490102294, 1489492056, 1489146214, 1488996898, 1487836270, 1487679238, 1487608135, 1486135833, 1486135831, 1485499621, 1483602378, 1480939014, 1480930692, 1480772053, 1478714056, 1475063435, 1474374128, 1471287515, 1471286235, 1470754384, 1469621456, 1469102821, 1469002404, 1468420343, 1468419387, 1468419242, 1468418750, 1468417262, 1468414617, 1468329183, 1468221936, 1467969766, 1467806108, 1467805914, 1467805781, 1467805474, 1467788577, 1467706157, 1467630892, 1467628966, 1467451711, 1467226414, 1467180754] [1497856315, 1497688102, 1497614594, 1497337111, 1493924259, 1492888300, 1492194473, 1492099489, 1491910949, 1491725464, 1491467821, 1491228362, 1490703933, 1490360502, 1490277769, 1490102294, 1489492056, 1489146214, 1488996898, 1487836270, 1487679238, 1487608135, 1486135833, 1486135831, 1485499621, 1483602378, 1480939014, 1480930692, 1480772053, 1478714056, 1475063435, 1474374128, 1471287515, 1471286235, 1470754384, 1469621456, 1469102821, 1469002404, 1468420343, 1468419387, 1468419242, 1468418750, 1468417262, 1468414617, 1468329183, 1468221936, 1467969766, 1467806108, 1467805914, 1467805781, 1467805474, 1467788577, 1467706157, 1467630892, 1467628966, 1467451711, 1467226414, 1467180754] 

here 2 times the same thing why he does it and he continues further in a couple of minutes there would be + 2 more, etc.

maybe it's not Parsa? and putting data in the cell

    2 answers 2

    You output data

     print(timeArray) 

    in cellForRowAt . The problem you have here is that the cells in the UITableView are reusable, so when scrolling, this method is constantly called and everything that you have written there is executed.

    There will be something heavy that will work for a long time - you will get a wild slowdown.

    All the preparatory work for processing the data that will be fed into the tableView is placed inside your URLSession.shared.dataTask... Those. What are you doing in the cell:

     let date = NSDate(timeIntervalSince1970: TimeInterval(timeArray[indexPath.row])) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" cell.dataLabel.text = dateFormatter.string(from: date as Date) 

    Initially, go and put in an array in the ready-made form, and in the cell just give the finished string:

     cell.dataLabel.text = formattedTimeStringArray[indexPath.row] 

    Etc.

    So the main thing - cells in UITableView reusable.

    • Thank you very much, I moved the date, but the program still slows down and basically nothing has changed (((( - Sergey
    • why does he constantly ask the array when scrolling? why it is not enough for him once as in any other programming language, litter for stupid questions, I just recently started using Swift and many things are unclear @VAndrJ - Sergey
    • @Sergey is not only a date to take out. Here I look pictures are loaded in cells. So, too, can not be done, it is necessary to do it in a separate thread, etc., while we will not talk about it. The simplest solution for you is to use the library for asynchronous loading of images, here’s a good option: github.com/rs/SDWebImage . - VAndrJ
    • Thank you very much. You helped me a lot. I will be very grateful if you can tell me something else in my code - Sergey
    • Please look here too. ( ru.stackoverflow.com/questions/682482/… ). Well, here again please. ( ru.stackoverflow.com/questions/683630/… ) - Sergey

    You just need to take everything from the cells to the method where the data is taken, since this is not the time it turns out this, judging by the code, but with the scroll.

    • under all you have 'if' too? then how to check for errors - Sergey
    • why does he constantly ask the array when scrolling? why is it not enough for him once in any other programming language, litter for stupid questions, I just recently started using Swift and many things are unclear @ Son'ka V - Sergey