1

Пытаюсь сделать подгрузку данных. Добавляю ячейки в UITableViewController через insertRows, однако при их добавлении наблюдается кратковременный лаг. Попытался записать, но на видео не очень заметно: https://gfycat.com/ru/ediblepoisedapatosaur

let currentCount = self.notifications.count
let newData = result.data!.return!
var newDataCount = 0
self.notificationsTable.beginUpdates()
self.notificationsTable.deleteRows(at: [IndexPath(row: currentCount, section: self.readedSection)], with: .none) // удаляю ячейку с UIActivityIndicator
self.notificationsTable.insertRows(at: newData.map {notification in
  let indexPath = IndexPath(row: currentCount + newDataCount, section: self.readedSection)
  newDataCount += 1
  return indexPath
}, with: .none)
self.notificationsTable.endUpdates()
self.viewWillLayoutSubviews()

override func viewWillLayoutSubviews() {
  notificationsTableHeight.constant = notificationsTable.contentSize.height
  super.updateViewConstraints()
  super.viewWillLayoutSubviews()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  if notifications.count == 0 && unreadNotifications.count == 0 {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "NotAvailableCell", for: indexPath) as? NotAvailableCell {
      cell.label.text = "Нет новых уведомлений"
      return cell
    }
  } else if indexPath.section == readedSection && notifications.count > 0 && notifications.count <= indexPath.item {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "LoadingCell", for: indexPath) as? LoadingCell {
      cell.spinner.startAnimating()
      return cell
    }
  } else {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationCell") as? NotificationCell {
      if indexPath.section == unreadedSection {
        cell.notification = unreadNotifications[indexPath.item]
      } else if indexPath.section == readedSection {
        cell.notification = notifications[indexPath.item]
      }
      return cell
    }
  }
  return UITableViewCell()
}

Как это подвисание можно пофиксить?

6
  • Можно newData.map сделать до beginUpdates. Ну и viewWillLayoutSubviews что-то наворочено. 17 сен 2020 в 9:15
  • Зачем notificationsTableHeight.constant = notificationsTable.contentSize.height super.updateViewConstraints()?
    – VAndrJ
    17 сен 2020 в 9:38
  • @VAndrJ таблица находится в UIScrollView, для этого и constraint меняется
    – WGPavell
    17 сен 2020 в 9:47
  • Для чего UIScrollView и возможно ли от него избавиться?
    – VAndrJ
    17 сен 2020 в 18:17
  • @VAndrJ избавился. Но теперь возникла другая проблема - ячейки "скачут" при скроллинге
    – WGPavell
    18 сен 2020 в 7:31

2 ответа 2

0

Убрал UIScrollView, в котором находилась таблица и теперь ситуация стала лучше. Однако, при скроллинге заметен "скачок" ячеек и чем ниже запускаешься, тем он сильнее.

0

Попробуйте проверить cell.spinner.startAnimating(). Возможно что то в главном потоке срабатывает. Попробуйте привязать анимацию к частоте обновления экрана. Но с этим надо быть аккуратным.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.