表视图很多

时间:2018-06-11 11:58:35

标签: swift xcode uitableview autolayout uitextview

我使用tableView仅显示文字。在单元格内部,我使用动态高度UITextViewlinkDetection,html文本转换为attributedString

我正在解析html文本到我的model类中的归属文本:

if let nodeValue = dict["blurb_link"]{
    blurb_link = nodeValue as? String
    let attrString = NSMutableAttributedString(attributedString: (blurb_link?.html2AttributedString)!)
    atr_blurb_link = attrString
}
html2AttributedString extension String中的attributedString从html文本返回textViewBlurbLink.attributedText = model.atr_blurb_link

我通过以下方式在细胞亚类中填充我的细胞,

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) }) 滚动有点不稳定且令用户感到不安。

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

编辑:

我的单元格布局是:

enter image description here

收到回复后,我已经在下面填写了tableView:

cellForRowAtIndexPath

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

setupCellData

tableViewCell子类textView内,我将文字设置为model class

EDIT2:

这是我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 }

driver.switchTo().frame(driver.findElement(By.id("frameId")))

1 个答案:

答案 0 :(得分:1)

可能出现的问题:

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

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