Как мне решить проблему специального джемтона, когда ячейка таблицы проскальзывает? - PullRequest
0 голосов
/ 04 апреля 2019

Когда количество моих ячеек относительно мало, табличное представление работает гладко, но когда мое число ячеек становится равным четырем, оно становится очень неровным.Мне нужно, чтобы мой табличный вид работал очень гладко.И есть некоторые предупреждения в Xcode.Когда я добавляю новую ячейку, она показывает

"2019-04-04 14: 00: 15.939875 + 0800 ToDoList [12259: 2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp> targetTimestamp; сдвиг вперед на 11,3833342019-04-04 14: 00: 15.943008 + 0800 ToDoList [12259: 2660300] [UIWorkIntervalTiming] workIntervalStart: startTimestamp> targetTimestamp; перемотка вперед на 5,616667 2019-04-04 14: 01: 05.601183 + 0800 ToDoList [12259: 2660]UIWorkIntervalTiming] workIntervalStart: startTimestamp> targetTimestamp; перемотка вперед на 2,150000*

Как я могу решить эту проблему?

Я ничего не пробовал, не знаю с чего начать.и это мой пользовательский интерфейс: 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)   
            }

        }
    }


}

1 Ответ

0 голосов
/ 04 апреля 2019

добавьте этот код при перезагрузке таблицы

TableView.reloadData()
 TableView.beginUpdates()
  TableView.endUpdates()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...