簡體   English   中英

當tableview細胞滑動時,如何解決特殊干擾問題?

[英]How do I solve the problem of special jamton when tableview cell slides?

當我的單元格數量相對較小時,表格視圖運行平穩,但當我的單元格數量變為四時,它變得非常不平滑。 我需要我的表視圖才能非常流暢地運行。 Xcode中有一些警告。 當我添加一個新單元格時,它會顯示出來

“2019-04-04 14:00:15.939875 + 0800 ToDoList [12259:2660300] [UIWorkIntervalTiming] workIntervalStart:startTimestamp> targetTimestamp;由11.383334向前滾動2019-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:2660300] [UIWorkIntervalTiming] workIntervalStart:startTimestamp> targetTimestamp;向前滾動2.150000 2019-04-04 14 :01:05.602619 + 0800 ToDoList [12259:2660300] [UIWorkIntervalTiming] workIntervalStart:startTimestamp> targetTimestamp;由43.433334向前滾動“。

我怎么解決這個問題?

我沒有嘗試過,我不知道從哪里開始。 這是我的用戶界面: 在此輸入圖像描述

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)   
            }

        }
    }


}

重新加載tableview時添加此代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM