2017-09-16 20 views
0

当我在tableview中向下滚动时,TextView加载缓慢并且滚动已经中断。带有延迟和缓慢的TextView加载

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if arrayofCellData[indexPath.row].isDesc == false{ 
      let cell = Bundle.main.loadNibNamed("imagesTableViewCell", owner: self, options: nil)?.first as! imagesTableViewCell 
      let indexA = self.arrayofCellData[indexPath.row].text 
      let path = "http://a"+indexA! 
      let url = URL(string: path) 
      cell.detailsImg.sd_setImage(with: url) 
      return cell 

     } 
      else { 
      let cell = Bundle.main.loadNibNamed("imagesTableViewCellDesc", owner: self, options: nil)?.first as! imagesTableViewCellDesc 

      let textD = self.arrayofCellData[indexPath.row].text! 
      let style = NSMutableParagraphStyle() 
      style.lineSpacing = 12 
      let attributes = [NSParagraphStyleAttributeName : style] 
      cell.descText.attributedText = NSAttributedString(string: textD, attributes:attributes) 
         cell.descText.textColor = UIColor.white 
         cell.descText.font = UIFont(name: (cell.descText.font?.fontName)!, size: 16) 
         cell.descText.textAlignment = .right 


      return cell 
     } 
    } 
+0

如何定义sd_setImage? –

+0

Hello @Lamiya,当你使用TableView时,不要使用Bundle.main.loadNibNamed,使用这一个dequeueReusableCellWithIdentifier。 –

+0

我认为延迟发生是因为它看起来好像每次构建单元时都重新加载图像而不是将它们缓存到任何地方。根据您拥有的图片数量,您可能希望在初始化表格时通过某种异步处理来加载其中的某些/全部图片,然后将其保存到数据模型中,然后从中加载它们,而不是创建http :每次请求。 – Sparky

回答

0

以下两行是您滞后的根本原因。您每次都尝试使用新的TableViewCell

let cell = Bundle.main.loadNibNamed("imagesTableViewCell", owner: self, options: nil)?.first as! imagesTableViewCell 

let cell = Bundle.main.loadNibNamed("imagesTableViewCellDesc", owner: self, options: nil)?.first as! imagesTableViewCellDesc 

因此,要解决这个问题,你必须使用dequeueReusableCellWithIdentifierif and else statement像下面

let cell = tableView.dequeueReusableCellWithIdentifier("imagesTableViewCell", forIndexPath: indexPath) as imagesTableViewCell 

let cell = tableView.dequeueReusableCellWithIdentifier("imagesTableViewCellDesc", forIndexPath: indexPath) as imagesTableViewCellDesc 

不要忘记注册您的笔尖在viewDidLoad()这样

tableView.register(UINib(nibName: "imagesTableViewCellDesc", bundle: nil), forCellReuseIdentifier: "imagesTableViewCellDesc") 
tableView.register(UINib(nibName: "imagesTableViewCell", bundle: nil), forCellReuseIdentifier:"imagesTableViewCell") 

据作为imageLoading而言,我不认为它会滞后你的滚动,因为我认为你使用的是SDWebImage库,它自动地ly缓存图像并异步工作。

+0

使用dequeueReusableCellWithIdentifier返回nil单元格 – Lamiya

+0

@Lamiya你有没有先在'viewDidLoad'中注册你的笔尖?如果不是,请先做它 –

+0

我已经完成,但仍然返回无 – Lamiya