表格视图卡住了很多

Posted

技术标签:

【中文标题】表格视图卡住了很多【英文标题】:Table view stucking a lot 【发布时间】:2018-06-11 11:58:35 【问题描述】:

我使用tableView 仅显示文本。在单元格内,我使用的是具有动态高度的UITextView 和带有转换为attributedStringhtml 文本的linkDetection

我正在将 html 文本解析为 model 类中的属性文本:

if let nodeValue = dict["blurb_link"]
    blurb_link = nodeValue as? String
    let attrString = NSMutableAttributedString(attributedString: (blurb_link?.html2AttributedString)!)
    atr_blurb_link = attrString

extension 中的String 中的html2AttributedString 从 html 文本返回 attributedString

我正在通过以下方式将我的单元格填充到单元格子类中,

textViewBlurbLink.attributedText = model.atr_blurb_link

但是正在发生的问题是,每当加载一个新单元格时,tableview 会卡住几分之一秒,这使得tableView 滚动有点生涩并对用户造成干扰。

单元格内没有图像,没有视频,没有繁重的任务,可能会导致滚动不顺畅。请告诉我这种生涩和打嗝的滚动可能是什么原因?

编辑:

我的单元格的布局是:

得到回复后,我在下面填充了 tableView:

TSNetworkManager.getDataFor(Request: dataRequest, success: response in
        //print(response.responseObject!)
        let model = TSLatestModel.init(WithDictionary:         response.responseObject as? [String : Any])
        completion(model)
        tblLatest.reloadData()
    , failure: error in
        onError(error)
)

cellForRowAtIndexPath内部

let modelLatestDetails = model?.data![indexPath.section].items![indexPath.row]
cell.setupCellData(model: modelLatestDetails!)

tableViewCell 子类的setupCellData 内,我将文本设置为textView

EDIT2:

这是我的 model class 的总代码库

class TSLatestModel: TSModelBase 
var data : [LatestListModel]?
override init(WithDictionary dictionary: [String : Any]?) 
    super.init(WithDictionary: dictionary)
    if let dict = dictionary
        if let nodeValue = dict["data"] 
            data = [LatestListModel]()
            for latest in nodeValue as! [Any]
                let model = LatestListModel.init(WithDictionary: latest as? [String:Any])
                data?.append(model)
            
        
    



class LatestListModel: NSObject 
var storyname : String?
var main_title : String?
var color : String?
var issuedate : String?
var formatted_issue_date : String?
var id : String?
var cat_name : String?
var cat_id : String?
var order : Int?
var items : [TSLatestDetailsModel]?
var itemsModified : [TSLatestDetailsModel]?
init(WithDictionary dictionary: [String : Any]?) 
    super.init()
    if let dict = dictionary 
        if let nodeValue = dict["items"] 
            items = [TSLatestDetailsModel]()
            itemsModified = [TSLatestDetailsModel]()
            for item in nodeValue as! [Any] 
                let model = TSLatestDetailsModel.init(WithDictionary: item as? [String : Any])
                items?.append(model)
                if !((item as! [String : Any])["one_liner"] as! String).isEmpty 
                    let filteredArray = itemsModified?.filter($0.one_liner == ((item as! [String : Any])["one_liner"] as! String))
                    if filteredArray?.count == 0 
                        let model = TSLatestDetailsModel.init(WithDictionary: item as? [String : Any])
                        itemsModified?.append(model)
                    
                
            
        

        if let nodeValue = dict["item_list"] 
            items = [TSLatestDetailsModel]()
            itemsModified = [TSLatestDetailsModel]()
            for item in nodeValue as! [Any] 
                let model = TSLatestDetailsModel.init(WithDictionary: item as? [String : Any])
                items?.append(model)
                if !((item as! [String : Any])["one_liner"] as! String).isEmpty 
                    let filteredArray = itemsModified?.filter($0.one_liner == ((item as! [String : Any])["one_liner"] as! String))
                    if filteredArray?.count == 0 
                        let model = TSLatestDetailsModel.init(WithDictionary: item as? [String : Any])
                        itemsModified?.append(model)
                    
                
            
        

        if let nodeValue = dict["_id"] 
            storyname = nodeValue as? String
        

        if let nodeValue = dict["order"] 
            order = nodeValue as? Int
        

        if let nodeValue = dict["category"] 
            cat_id = (nodeValue as! [Any])[0] as? String
        

        if let dictStoryType = dict["_id"] as? [String : Any] 
            if let nodeValue = dictStoryType["issuedate"] 
                issuedate = nodeValue as? String
            
            if let nodeValue = dictStoryType["formated_issue_date_title"] 
                formatted_issue_date = nodeValue as? String
            
            if let nodeValue = dictStoryType["id"] 
                id = nodeValue as? String
            
            if let nodeValue = dictStoryType["category_name"] 
                cat_name = nodeValue as? String
            

        

        if let nodeValue = dict["name"] 
            storyname = nodeValue as? String
        

        if let nodeValue = dict["story"] 
            storyname = nodeValue as? String
        

        if let nodeValue = dict["main_title"] 
            main_title = nodeValue as? String
        

        if let nodeValue = dict["color"] 
            color = nodeValue as? String
        

    



class TSLatestDetailsModel: NSObject 

var __v : Int?
var _id : String?
var title : String?
var topic_key : String?
var blurb : String?
var blurb_link : String?
var atr_blurb_link : NSMutableAttributedString?
var formated_issue_date : String?
var formated_issue_date_item : String?
var formated_issue_date_title : String?
var issue_link : String?
var issue_title : String?
var issue_date : String?
var one_liner : String?
var main_title : String?
var source : String?
var source_link : String?
var isActive : Bool?
var isDeleted : Bool?
var isfavourite : Bool?
var story_order_number : Int?
var story_type : String?
var categories : [String]?
var story_type_model : TSStoryTypeDetailsModel?
var favourite_category_id : String?

init(WithDictionary dictionary: [String : Any]?) 
    super.init()
    if let dict = dictionary
        if let nodeValue = dict["__v"]
            __v = nodeValue as? Int
        
        if let nodeValue = dict["_id"]
            _id = nodeValue as? String
        
        if let nodeValue = dict["title"]
            title = nodeValue as? String
        
        if let nodeValue = dict["topic_key"]
            topic_key = nodeValue as? String
        
        if let nodeValue = dict["blurb"]
            blurb = nodeValue as? String
        
        if let nodeValue = dict["blurb_link"]
            blurb_link = nodeValue as? String
            let attrString = NSMutableAttributedString(attributedString: (blurb_link?.html2AttributedString)!)
            atr_blurb_link = attrString
        
        if let nodeValue = dict["formated_issue_date"]
            formated_issue_date = nodeValue as? String
        
        if let nodeValue = dict["issue_date"]
            issue_date = nodeValue as? String
        
        if let nodeValue = dict["issue_link"]
            issue_link = nodeValue as? String
        
        if let nodeValue = dict["issue_title"]
            issue_title = nodeValue as? String
        
        if let nodeValue = dict["one_liner"]
            one_liner = nodeValue as? String
        
        if let nodeValue = dict["main_title"]
            main_title = nodeValue as? String
        
        if let nodeValue = dict["source"]
            source = nodeValue as? String
        
        if let nodeValue = dict["source_link"]
            source_link = nodeValue as? String
        
        if let nodeValue = dict["isActive"]
            isActive = nodeValue as? Bool
        
        if let nodeValue = dict["isfavourite"]
            isfavourite = nodeValue as? Bool
        
        if let nodeValue = dict["story_order_number"]
            story_order_number = nodeValue as? Int
        
        if let nodeValue = dict["story_type"]
            story_type = nodeValue as? String
        
        if let nodeValue = dict["formated_issue_date_title"]
            formated_issue_date_title = nodeValue as? String
        
        if let nodeValue = dict["formated_issue_date_item"]
            formated_issue_date_item = nodeValue as? String
        
        if let nodeValue = dict["favourite_category_ids"] 
            if (nodeValue as! [String]).count > 0 
                favourite_category_id = (nodeValue as! [String])[0]
            
        
        if let nodeValue = dict["story_type"] 
            let model = TSStoryTypeDetailsModel.init(WithDictionary: nodeValue as? [String : Any])
            story_type_model = model
        
        if let nodeValue = dict["category"] 
            categories = [String]()
            for category in nodeValue as! [String] 
                categories?.append(category)
            
        
    


cellForRowAtIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "TSArchiveDetailsCell") as! TSArchiveDetailsCell
    cell.delegate = self


    if !((favoriteDetilsModel?.data![indexPath.section].items![indexPath.row].source!.isEmpty)!) 
        cell.viewFav.isHidden = false
     else 
        if favoriteDetilsModel?.data![indexPath.section].storyname?.lowercased() == "brush up" 
            cell.viewFav.isHidden = false
         else 
            cell.viewFav.isHidden = true
        
    

    if isSayItOnRounds 
        let modelLatestDetails = favoriteDetilsModel?.data![indexPath.section].items?.filter($0._id! == selectedItemId)[0]
        cell.setupCellData(model: modelLatestDetails!)
     else if isBrushUp 
        let thirdBrushUpItemModel = favoriteDetilsModel?.data![indexPath.section].items![2]
        if thirdBrushUpItemModel?._id == selectedItemId 
            let modelLatestDetails = favoriteDetilsModel?.data![indexPath.section].items?.filter($0._id == selectedItemId)
            cell.setupCellData(model: modelLatestDetails![0])
         else 
            let modelLatestDetails = favoriteDetilsModel?.data![indexPath.section].items![indexPath.row]
            cell.setupCellData(model: modelLatestDetails!)
        
     else 
        let modelLatestDetails = favoriteDetilsModel?.data![indexPath.section].items![indexPath.row]
        cell.setupCellData(model: modelLatestDetails!)
    
    return cell

【问题讨论】:

在此处添加此单元格的完整代码。 model?.data![indexPath.section].items![indexPath.row] - 数据和项目是简单的数组还是你的重载“[..]”(下标)? 有嵌套的字典数组先生! 有多个部分,部分下有多行。 参见例如我的回答是:***.com/a/50519089/341994。但不要过早优化;按照我之前说的做,用 Instruments 找出问题是什么,这样你才能解决正确的问题。 【参考方案1】:

可能的问题:

    单元格布局。我需要看看,你如何制作布局和重用单元格。 同时解析数据和绘制单元格。 您需要在 (tableView.datasource = self or tableView.reloadData()) 之前创建所有数据。因此,您的 [AttributedString] 必须在重新加载 tableView 之前准备好。不要解析cellfoRowAtIndexPath中的数据。

此外,请检查 tableView 在单元格生命周期内对每个单元格仅调用一次cellfoRowAtIndexPath

【讨论】:

Vasily Bodnarchuk:我已经编辑了我的问题,请您看一下。

以上是关于表格视图卡住了很多的主要内容,如果未能解决你的问题,请参考以下文章

表格的一用特殊符号就会卡住

拉动刷新后,带有 UiRefreshControl 的 Tableview 卡住了

Wordpress - 联系表格 7 - 卡住发送联系表格

将视图从storyBoard复制到另一个时xcode卡住了?

在 NSUserDefaults 的表格视图单元格中保存收藏按钮的状态

我的 iPhone 应用程序卡住了