Ios 如何在tableview底部添加新行-聊天信息
每次用户键入消息并单击“发送”时,我都使用以下代码添加新消息。它工作得很好。但问题是,新消息会插入到表视图的顶部。我想把它插在底部Ios 如何在tableview底部添加新行-聊天信息,ios,swift,uitableview,Ios,Swift,Uitableview,每次用户键入消息并单击“发送”时,我都使用以下代码添加新消息。它工作得很好。但问题是,新消息会插入到表视图的顶部。我想把它插在底部 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var messagesTable: UITableView! @IBOutlet var messageTextBox:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var messagesTable: UITableView!
@IBOutlet var messageTextBox: UITextField!
var messageArray = [String]()
@IBAction func sendMessage(sender: AnyObject) {
messageArray.append(messageTextBox.text!)
messagesTable.reloadData()
messageTextBox.text = ""
}
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//self.messagesTable.contentInset = UIEdgeInsetsMake(messagesTable.frame.height,0,0,0)
let cell = NSBundle.mainBundle().loadNibNamed("AgentMessageText", owner: self, options: nil)?.first as! AgentMessageText
cell.messageText.text = messageArray[indexPath.row]
return cell
}
}
下面的代码将行插入到底部。但新插入的行位于视点下方。也就是说,我们每次都要滚动查看它
self.messagesTable.contentInset = UIEdgeInsetsMake(messagesTable.frame.height,0,0,0)
此问题的常见解决方案是翻转tableview(使单元格贴到底部),然后翻转单元格,使内容正确定向 例如:
override func viewDidLoad() {
super.viewDidLoad()
//flips the tableview (and all cells) upside down
messagesTableView.transform = CGAffineTransformMakeScale(1, -1)
}
适用于Swift 4
需要明确的是,对于Swift 4.0,上述转换函数如下所示
CGAffineTransform(scaleX: 1, y: -1)
您必须使用
scrollToRow
滚动至您的邮件(根据需要使用.bottom/.top/等)
也可以只向表视图添加一条(新)消息(单元格),而不是重新加载整个表视图
@IBAction func sendMessage(sender: AnyObject) {
messageArray.append(messageTextBox.text!)
messagesTable.reloadData()
let indexPath = IndexPath(item: messageArray.count, section: 0)
messagesTable.scrollToRow(at: indexPath, at: .bottom, animated: true)
messageTextBox.text = ""
}
用于在聊天应用程序中滚动到底部
For Swift 3.0
编写一个函数:
func scrollToBottom(){
DispatchQueue.main.async {
let indexPath = IndexPath(row: self.dataArray.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
并在重新加载tableview数据后调用它
tableView.reloadData()
scrollToBottom()
斯威夫特5。编写一个函数:
self.tableView.reloadData() let index =
self.tableView.indexPathsForVisibleRows self.tableView.scrollToRow(at: (index?.last)!, at: .top, animated: true)
查看代码时,您的消息将插入底部而不是顶部。您面临的问题是您必须滚动查看它们,对吗?将消息添加到您的消息数组中,并使用
insertRowsAtIndexPaths
插入您的行,然后使用scrollToRowatinedexpath
滚动到新添加的消息,以及为什么要将消息添加到顶部,您必须向我们展示如何为新消息更新数据源数组。正如Inder Kumar Rathore所说,您的单元格不应添加在底部,而应查看单元格的行路径。也不需要重新加载整个TableView,只需使用ScrollToRowatineXpath并滚动到最后一个即可element@InderKumarRathore exactly@NSNoob你能告诉我完整的代码吗?我不明白是的。如果您能给我看一下代码,那将是一个很大的帮助。当我对UICollectionView使用相同的方法时,CollectionView是翻转的,但单元格不是。有什么解决办法吗?我对它的实际效果感到恼火,我实现了它。它很好用。但我的collectionview一直在顶部无限滚动。此解决方案仅适用于表视图吗?我收到一个错误:“使用未解析标识符'indexPath'是否使用swift 3?然后检查如何在SWIFT2中创建indexPath如果我在滚动我的tableView时使用此函数接收消息,是否有任何可能的解决方案??Inder,您说您只是重新加载一个单元格,但代码中有行messagesTable.reloadData(),可以重新加载整个表
self.tableView.reloadData() let index =
self.tableView.indexPathsForVisibleRows self.tableView.scrollToRow(at: (index?.last)!, at: .top, animated: true)