TextView加载延迟和慢

时间:2017-09-16 12:57:48

标签: ios swift textview tableview

当我在tableview中向下滚动时,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
        }
    }

1 个答案:

答案 0 :(得分: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

因此,要解决此问题,您必须在dequeueReusableCellWithIdentifier中使用if 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库自动缓存图像并异步工作。

相关问题