简体   繁体   English

如何迅速滚动scrollToRow(at:at:animated :)?

[英]How does it work scrollToRow(at:at:animated:) in swift?

I am creating a chat interface. 我正在创建一个聊天界面。

User's message is put in a new UITable view cell. 用户的消息将放入新的UITable视图单元格中。

And when update the table view, I use the following code. 当更新表格视图时,我使用以下代码。

extension UITableView {

    func scrollToBottom() {
        let rows = self.numberOfRows(inSection: 0)

        if rows > 0 {
            let indexPath = IndexPath(row: rows - 1, section: 0)
            self.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }
}

Actually this works a little better, but there is something strange. 其实效果更好一些,但是有些奇怪。

When I turn off the app and turn it on again, or after exiting the screen and entering again, the following issues arise. 当我关闭该应用程序并再次打开它时,或者退出屏幕并再次进入后,会出现以下问题。

The issue is that when I add a new cell, it goes up to the first cell in the table view and back down to the last cell. 问题是,当我添加一个新单元格时,它会升至表视图中的第一个单元格,然后又回到最后一个单元格。

As the number of cells increases, the scrolling becomes too fast and too messy. 随着单元格数量的增加,滚动变得太快且太混乱。

I just want to keep updating the last cell that is newly registered. 我只想继续更新新注册的最后一个单元格。

What should I do? 我该怎么办?

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
        let numberOfChars = newText.characters.count // for Swift use count(newText)
        let typedStr : String = textView.text
        var compArr = typedStr.componentsSeparatedByString("\n")
        _ = newText.componentsSeparatedByString("\n")
        self.sendBtn.setTitle("Send", forState: .Normal)

        if numberOfChars > 0 {

            self.sendBtn.enabled = true
        }
        else {

            self.TextViewHeight.constant = TEXTVIEW_OFFSET
            self.sendBtn.enabled = false
        }
        let  char = text.cStringUsingEncoding(NSUTF8StringEncoding)!
        let isBackSpace = strcmp(char, "\\b")

        if text == "\n"
        {
            print("Return_Pressed")
            if  compArr.count == 1 || compArr.count == 2 || compArr.count == 3
            {
                self.TextViewHeight.constant =  self.TextViewHeight.constant + 25
                self.tableViewScrollToBottom(false)
            }
            else
            {

            }
        }
    }



func tableViewScrollToBottom(animated: Bool) {

      dispatch_async(dispatch_get_main_queue()) {

                let numberOfSections = self.ChatTableView.numberOfSections
                let numberOfRows = self.ChatTableView.numberOfRowsInSection(numberOfSections-1)

                if numberOfRows > 0 {

                    let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
                    self.ChatTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
                }

            }
    }

Try calling your scrollToBottom function in viewWillAppear method. 尝试在viewWillAppear方法中调用您的scrollToBottom函数。 Maybe it just needs to update the number of rows when you return to your TableViewController. 当您返回TableViewController时,也许只需要更新行数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM