UICollectionView在滚动时出现故障

时间:2019-05-20 03:22:00

标签: ios arrays swift uicollectionview contacts

我正在实现UICollectionView来加载屏幕上的所有联系信息。保持主线程和后台线程干净,我将数据加载到collectionview上。现在,一旦加载数据并开始滚动,它就会给我帧的极端滞后/抖动。我不确定为什么会这样。我所有的数据源都在后台线程上。

下面是代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let data : PhoneContact!
    data = allContacts[indexPath.row]
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: friendsCellID, for: indexPath) as! FriendsCell
    cell.label.text = data.name
    return cell
}

// Cell has only label
override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor(rgb: (r: 232, g: 231, b: 222))
    setLabel()
}

allContact有一个吸气剂,每次从电话簿中获取联系人。我想这可能是个问题。但不确定。我可以看看吗?我想我不是在想什么,而最终会得到错误的结果。

1 个答案:

答案 0 :(得分:1)

您的示例是Shift + ],而不是UICollectionViewController。如果您使用了UITableViewController,则可以尝试实现UITableViewController委托来处理长时间运行的操作,但是我认为这不是正在发生的事情。

如果您要从情节提要中的集合视图出队,则您应该具有初始化程序。相反,由于您在初始化程序中要做的只是设置标签和背景色,因此应该在情节提要中完成,并且应该在单元格与其标签之间连接一个插座。