ios - tableview cell滑动时出现特殊jamton的问题如何解决?

标签 ios swift xcode image uitableview

当我的单元格数量比较少时,表格 View 运行流畅,但是当我的单元格数量变成四个时,它就变得非常不流畅。我需要我的表格 View 运行得非常顺利。并且Xcode中有一些警告。当我添加新单元格时,它显示

"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" .

如何解决这个问题?

我什么都没有尝试过,我不知道从哪里开始。 这是我的用户界面: 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)   
            }

        }
    }


}

最佳答案

重新加载表格 View 时添加此代码

TableView.reloadData()
 TableView.beginUpdates()
  TableView.endUpdates()

关于ios - tableview cell滑动时出现特殊jamton的问题如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55508971/

相关文章:

ios - 应用程序包不包含有效的 IOS 标识符

ios - 在 Swift 3 中编写 UnsafeMutablePointer

ios - 简单的Webview:navigationDelegate在启动时崩溃

ios - 在 UISCROLLVIEW 中滚动内容时,背景图像也会滚动

ios - 从另一个 View Controller 以编程方式选择 TableView 单元

ios - 使用 Storyboard在 UILabel 中的文本前后添加一个空格

ios - 制图将约束设置为变量

ios - 现有的 Xcode/Swift3 应用程序突然无法识别 Parse 框架

ios - Xcode 不在模拟器上运行该应用程序。 (应用程序意外退出)

ios - Socket IO Swift 客户端在连接后添加身份验证 header