0

When the number of my cells is relatively small, the table view runs smoothly, but when my number of cells becomes four, it becomes very unsmooth. I need my table view to run very smoothly. And there are some warnings in Xcode. when I add a new cell, it shows

"2019-04-04 14:00:15.939875+0800 ToDoList[12259:2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp > targetTimestamp; rolling forward by 11.383334 2019-04-04 14:00:15.943008+0800 ToDoList[12259:2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp > targetTimestamp; rolling forward by 5.616667 2019-04-04 14:01:05.601183+0800 ToDoList[12259:2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp > targetTimestamp; rolling forward by 2.150000 2019-04-04 14:01:05.602619+0800 ToDoList[12259:2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp > targetTimestamp; rolling forward by 43.433334" .

How can I solve this problem?

Nothing I have tried, I don't know where to begin. and this my UI: enter image description here

import UIKit

class TableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {    
@IBOutlet var emptyItemView: UIView!

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if toDoItems.count == 1 {
            return 1
        } else if toDoItems.count == 2 {
            return 2
        } else if toDoItems.count == 0 {
            return 0
        } else {
            return 3
        }

    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! imageViewCollectionViewCell

        cell.imageView.image = UIImage(data: toDoItems[indexPath.row].imageDataOfItem)
        cell.generalLabel.text = toDoItems[indexPath.row].themeOfItem
        cell.titleLabel.text = toDoItems[indexPath.row].nameOfItem
        cell.subTitleLabel.text = toDoItems[indexPath.row].descriptionOfItem

        if toDoItems[indexPath.row].descriptionOfItem == "" {
            cell.subTitleLabel.isHidden = true
        }

        cell.layer.cornerRadius = 10.0
        cell.layer.masksToBounds = true

        return cell
    }

    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .clear
        }
    }


    // 第二组需要自定义cell,这里使用XIB来完成
    let nib = UINib(nibName: "BookTableViewCell", bundle: nil)


    override func viewDidLoad() {
        super.viewDidLoad()
        // 必须带上这两句话才有数据
        collectionView.delegate = self
        collectionView.dataSource = self
        navigationItem.leftBarButtonItem = editButtonItem
        // 需要用代码注册Nib
        self.tableView.register(nib, forCellReuseIdentifier: "TableViewCell")

        if let savedToDoLtems = ToDoItem.loadToDoItems() {
            toDoItems = savedToDoLtems
        } else {
            toDoItems = ToDoItem.loadSampleToDoItems()
        }

        tableView.backgroundView = emptyItemView
        tableView.backgroundView?.isHidden = true

    }

    override func viewWillAppear(_ animated: Bool) {
        collectionView.reloadData()
        tableView.reloadData()
    }


    /**
     * 这里是第二部分
     * 我想要在 CollectionView 下方展示一系列项目,使用 TableViewCell 来显示
     * 想要在静态的 cell 里面实现动态的 cell 需要使用 xib 文件
     */


    // 以下代理方法必须实现
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 1) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as? TableViewCell
            cell?.classLabel.text = toDoItems[indexPath.row].themeOfItem
            cell?.titleLabel.text = toDoItems[indexPath.row].nameOfItem
            cell?.subTitle.text = toDoItems[indexPath.row].descriptionOfItem

            let image = UIImage(data: toDoItems[indexPath.row].imageDataOfItem)
            cell?.backgroundImage.image = image

            return cell!
        }

        return super.tableView(tableView, cellForRowAt: indexPath)
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 1 {
            return toDoItems.count //这里返回第二组的行数
        }
        return super.tableView(tableView, numberOfRowsInSection: section)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {

        if toDoItems.count > 0 {
            tableView.backgroundView?.isHidden = true
            tableView.separatorStyle = .singleLine
            tableView.isScrollEnabled = true
        } else {
            tableView.backgroundView?.isHidden = false
            tableView.separatorStyle = .none
            tableView.isScrollEnabled = false
        }
        return 2
    }

    override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
        if indexPath.section == 1 {
            return super.tableView(tableView, indentationLevelForRowAt: IndexPath(row: 0, section: 1))
        }
        return super.tableView(tableView, indentationLevelForRowAt: indexPath)
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 1 {
            return 120
        }
        return super.tableView(tableView, heightForRowAt: indexPath)
    }

    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if indexPath.section == 1 {
            return true
        } else {
            return false
        }
    }
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            if editingStyle == .delete {
                toDoItems.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: .fade)
                self.collectionView.reloadData()
                ToDoItem.saveToDoItems(toDoItems)
            }
        }

    }



    // --- 这个功能为了实现点击每一个cell实现跳转 ---
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            let todoitem = toDoItems[indexPath.row]
            self.performSegue(withIdentifier: "EachItem", sender: todoitem)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.

        if segue.identifier == "EachItem" {
            let todoTableViewController = segue.destination as! ToDoTableViewController
            let indexPath = tableView.indexPathForSelectedRow!
            let selectedToDoItem = toDoItems[indexPath.row]
            todoTableViewController.todoItem = selectedToDoItem
            todoTableViewController.todos = selectedToDoItem.todos
            todoTableViewController.positionOfToDoItem = indexPath.row
        }

        if segue.identifier == "CollectionSegue" {
            if let indexPaths = collectionView.indexPathsForSelectedItems {
                let destinationController = segue.destination as! ToDoTableViewController
                destinationController.todoItem = toDoItems[indexPaths[0].row]
                destinationController.todos = toDoItems[indexPaths[0].row].todos
                destinationController.positionOfToDoItem = indexPaths[0].row
                collectionView.deselectItem(at: indexPaths[0], animated: false)
            }
        }

    }

    @IBAction func unwindToToDoItem(segue: UIStoryboardSegue) {
        if segue.identifier == "SaveNewItemSegue" {
            let sourceViewController = segue.source as! EditItemTableViewController

            if let item = sourceViewController.toDoItem {
                let newIndexPath = IndexPath(row: toDoItems.count, section: 1)
                toDoItems.append(item)
                tableView.insertRows(at: [newIndexPath], with: .automatic)
                ToDoItem.saveToDoItems(toDoItems)   
            }

        }
    }


}
Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
Huang Runhua
  • 146
  • 6
  • I notice this issued both `UICollectionView` view and `UITableView` for Xcode10.2 but It will be smooth on real device more check this one https://stackoverflow.com/a/55860774/4415445 – Nazmul Hasan Apr 26 '19 at 11:13

1 Answers1

0

add this code when you are reloading tableview

TableView.reloadData()
 TableView.beginUpdates()
  TableView.endUpdates()
hotfix
  • 3,376
  • 20
  • 36
Siddhant Nigam
  • 462
  • 3
  • 13
  • I think that is because I use the Xib file in my second section, can you tell my how to add a table view in a static cell without using Xib file? – Huang Runhua Apr 04 '19 at 08:15
  • You can drag and drop the table view in cell and make your cell height automatic dimensions and you take its outlet in UItableViewcell class. – Siddhant Nigam Apr 04 '19 at 08:51