在滚动时插入新单元格时,Tableview会出现混乱

时间:2017-03-27 15:57:30

标签: ios swift uitableview ios10 uiscrollviewdelegate

当用户向下滚动时,我在0位置插入新行。但是tableview的混蛋很少。以下是我的代码。

class SearchViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var chatItems:NSMutableArray = []

    var endDraggingPoint:CGFloat = 0;

    var beginDraggingPoint:CGFloat = 0;

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        chatItems = ["SearchBarCell","ChatCell"];

        for value in 1...100{

            chatItems.add(String(value))

        }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //MARK: - Tableview delegate Methods

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
     {
        return chatItems.count
    }


    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)


     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
     {

        var cell:UITableViewCell!

        var cellIdentyfier: NSString


        let itemName:NSString = chatItems.object(at: indexPath.row) as! NSString;

        if itemName == "SearchBarCell"
        {
            cellIdentyfier = "SearchBarCell"

        }else if itemName == "ArchiveLabelCell" {
            cellIdentyfier = "ArchiveLabelCell"
        }else {
            cellIdentyfier = "ChatCell"
        }

        cell = tableView.dequeueReusableCell(withIdentifier: cellIdentyfier as String)

        if(cell == nil)
        {
            cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier:cellIdentyfier as String)
        }

        return cell;
    }


    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        print("scrollViewDidScroll: \(scrollView.contentOffset.y)");

        if endDraggingPoint == 0 && scrollView.contentOffset.y <= -30 {

            self.addArchiveCell(scrollView: scrollView);

        }else if scrollView.contentOffset.y > 50 {

            self.removeArchiveCell(scrollView: scrollView)
        }
    }




    func removeArchiveCell(scrollView:UIScrollView) {

        if chatItems.contains("ArchiveLabelCell") {
            print("Removing archive cell: \(scrollView.contentOffset.y)");
            chatItems.removeObject(at: 0);
            tableView.deleteRows(at: [IndexPath.init(row: 0, section: 0)], with: .none)
        }
    }

    func addArchiveCell(scrollView:UIScrollView) {

        if beginDraggingPoint <= 50 {

            if !chatItems.contains("ArchiveLabelCell") {
                self.chatItems.insert("ArchiveLabelCell", at: 0)
                print("Adding archive cell: \(scrollView.contentOffset.y)");
                //self.tableView.beginUpdates()
                self.tableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .none)
                //self.tableView.endUpdates()

            }
        }
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
    {
        endDraggingPoint = 0
        beginDraggingPoint = scrollView.contentOffset.y

    }


}

有人可以告诉我为什么tableview在向下滚动并插入新行时会出现混蛋?

0 个答案:

没有答案