Tableview滚动有点烦躁。我怎样才能使它滑腻

时间:2018-07-08 10:43:35

标签: ios iphone swift uitableview smooth-scrolling

这是单元格中用于行方法的代码:-

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

    let cell = tableView.dequeue(.journalListingCell, for: indexPath) as! JournalListViewCell
    cell.delegate = self
    //fetching the category model as per the category id 
    if let category = categories?.first(where: { $0.categoryID == dreamJournalArray[indexPath.row].categoryID }) {

        cell.configureCell(dreamJournalArray[indexPath.row], category)
    }

    return cell
}

此代码只是将类别数组中的类别ID与主数据模型中的类别ID匹配,并将该类别与数据模型一起传递给配置单元格模型。

configure单元格方法如下:-

 typealias Model = DreamJournalModel
 func configureCell(_ model: Model, _ category: CategoryModel) {

    //setting the gradient colors
    verticalView.backgroundColor = category.categoryGradientColor(style: .topToBottom, frame: verticalView.frame, colors: [UIColor.init(hexString: category.initialGradientColor)!,  UIColor.init(hexString: category.lastGradientColor)!])
    horizontalVIew.backgroundColor = category.categoryGradientColor(style: .leftToRight, frame: self.frame, colors: [ UIColor.init(hexString: category.lastGradientColor)!, UIColor.init(hexString: category.initialGradientColor)!])
    timelineCircleView.backgroundColor = category.categoryGradientColor(style: .topToBottom, frame: timelineCircleView.frame, colors: [UIColor.init(hexString: category.initialGradientColor)!,  UIColor.init(hexString: category.lastGradientColor)!])

    // setting the intention matching image as per the rating
   intentionImageView.image = UIImage.init(named: "Match\(model.intentionMatchRating)")

    // setting up the date of the journal recorded
   let date = Date(unixTimestamp: model.createdAt!)
    dateMonthLabel.text = (date.monthName(ofStyle: .threeLetters)).uppercased()
    dateNumberLabel.text = String(date.day)

    //setting the titles and labels
    dreamTitleLabel.text = model.title
    dreamTagsLabel.text = model.tags

    // setting the lucid icon
    gotLucidImageview.isHidden = !(model.isLucid!)

    //setting the buttons text or image
    if model.recordingPath != nil {
        addRecordButton.setTitle(nil, for: .normal)
        addRecordButton.backgroundColor = UIColor.clear
        addRecordButton.layer.cornerRadius = 0
        addRecordButton.setImage(LRAsset.cardRecording.image, for: .normal)

    }
    else{
        addRecordButton.setTitle(" + RECORD ", for: .normal)
        addRecordButton.backgroundColor = UIColor.blackColorWithOpacity()
        addRecordButton.layer.cornerRadius = addRecordButton.bounds.height/2
        addRecordButton.setImage(nil, for: .normal)

    }

    if model.note != nil {
        addNoteButton.setTitle(nil, for: .normal)
        addNoteButton.backgroundColor = UIColor.clear
        addNoteButton.layer.cornerRadius = 0
        addNoteButton.setImage(LRAsset.cardNote.image, for: .normal)

    }
    else{
        addNoteButton.setTitle(" + NOTE ", for: .normal)
        addNoteButton.backgroundColor = UIColor.blackColorWithOpacity()
        addNoteButton.layer.cornerRadius = addRecordButton.bounds.height/2
        addNoteButton.setImage(nil, for: .normal)

    }

    if model.sketchPath != nil {
        addSketchButton.setTitle(nil, for: .normal)
        addSketchButton.backgroundColor = UIColor.clear
        addSketchButton.layer.cornerRadius = 0
        addSketchButton.setImage(LRAsset.CardSketch.image, for: .normal)
    }
    else{
        addSketchButton.setTitle(" + SKETCH ", for: .normal)
        addSketchButton.backgroundColor = UIColor.blackColorWithOpacity()
        addSketchButton.layer.cornerRadius = addSketchButton.bounds.height/2
        addSketchButton.setImage(nil, for: .normal)

    }
}

在这种方法中,我将根据类别在单元格中设置渐变颜色。然后根据上述条件在底部设置按钮状态。

这是我的表格视图的样子:- enter image description here

单元格底部的3个按钮位于Stackview中,其外观根据条件而动态变化。

tableview滚动不流畅,并且用肉眼可以看到滞后现象。单元格的高度固定为205.0,并在行索引方法的高度中定义。 我不知道我在哪里获取数据并将其馈送到单元逻辑是错误的。 请指导此处是否可以改善滚动效果。 预先感谢。

1 个答案:

答案 0 :(得分:2)

您在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell内编写了太多代码,这些代码不应该在那里。例如,可以设置拐角半径,背景颜色,渐变。每次都调用它们。在awakeFromNib方法内创建单元格时,可以实现这些功能。

但这并不是您的tableview出现滚动问题的主要原因。因为要在该方法中添加图像。您可以使用iOS 10新的tableView(_:prefetchRowsAt:)方法,在显示图像之前,最好在此位置加载图像或为单元格准备数据,例如在其中取像,然后在出队方法之前调用。

这是Apple文档的链接表单:

https://developer.apple.com/documentation/uikit/uitableviewdatasourceprefetching/1771764-tableview

  

当用户滚动时,表格视图会调用此方法,从而为可能在不久的将来显示的单元格提供索引路径。您对该方法的实现负责启动任何昂贵的数据加载过程。数据加载必须异步执行,并且结果可用于表视图数据源上的tableView(_:cellForRowAt :)方法。集合视图不会针对需要立即使用的单元格调用此方法,因此您的代码不得依赖在此方法上加载数据。提供的索引路径的顺序表示优先级。

https://developer.apple.com/documentation/uikit/uitableviewdatasourceprefetching