swift 消息传递应用程序,uitableview 无法正确响应键盘?
swift messaging app, uitableview not responding correctly to keyboard?
一个类似的问题是 ,除了我没有估计的行高,我将实际高度存储到字典中,然后使用它们,或者使用 UITableViewAutomaticDimension
.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeightDict[indexPath.row] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
所以我现在正在 Swift 4 中制作一个消息传递应用程序。当用户显示键盘时,我希望消息随着键盘向上移动。所以为了做到这一点,我有一个变量keyboardHeight
,它正确地获取键盘的高度:
let keyboardHeight: CGFloat = ((userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height)!
我将 table 视图的高度更改为 keyboardHeight
:
self.messageTableViewHeight.constant -= keyboardHeight
因此用户可以看到 所有 的消息。为了 向上移动 带有键盘动画的消息,我更改了 table 视图的 contentOffset:
self.messageTableView.contentOffset.y += keyboardHeight
所有这些都在 UIView.animationWithDuration
中,我在之后调用 self.view.layoutIfNeeded()
,所以转换和一切正常。 但是,当我发送消息时没有滚动到底部,messageTableView 的内容就往下跳了??? 而且好像向下移动了keyboardHeight
,至少当我看到它的时候。我正在使用 messageTableView.reloadData()
。这是一些图片。
Before message sent
它在 "Hi" 和 "Testing" 消息之间跳转。
After message sent
一旦您重新加载表视图的数据messageTableView.reloadData()
。每行都重新加载,但无法以某种方式计算实际内容大小。您可以尝试重新加载主队列中的数据。
DispatchQueue.main.async {
messageTableView.reloadData()
}
如果还是不行,你可以在上面的代码中再添加一件事。假设您的 class 中有一组消息。 var messages
那么你就可以使用这段代码在tableview中正确显示数据了。
DispatchQueue.main.async {
messageTableView.reloadData()
if messages.count > 0 {
let indexPath = IndexPath(row: messages.count - 1, section: 0)
self.messageTableView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
希望这对您有所帮助。编码愉快。
一个类似的问题是 UITableViewAutomaticDimension
.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeightDict[indexPath.row] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
所以我现在正在 Swift 4 中制作一个消息传递应用程序。当用户显示键盘时,我希望消息随着键盘向上移动。所以为了做到这一点,我有一个变量keyboardHeight
,它正确地获取键盘的高度:
let keyboardHeight: CGFloat = ((userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height)!
我将 table 视图的高度更改为 keyboardHeight
:
self.messageTableViewHeight.constant -= keyboardHeight
因此用户可以看到 所有 的消息。为了 向上移动 带有键盘动画的消息,我更改了 table 视图的 contentOffset:
self.messageTableView.contentOffset.y += keyboardHeight
所有这些都在 UIView.animationWithDuration
中,我在之后调用 self.view.layoutIfNeeded()
,所以转换和一切正常。 但是,当我发送消息时没有滚动到底部,messageTableView 的内容就往下跳了??? 而且好像向下移动了keyboardHeight
,至少当我看到它的时候。我正在使用 messageTableView.reloadData()
。这是一些图片。
Before message sent
它在 "Hi" 和 "Testing" 消息之间跳转。
After message sent
一旦您重新加载表视图的数据messageTableView.reloadData()
。每行都重新加载,但无法以某种方式计算实际内容大小。您可以尝试重新加载主队列中的数据。
DispatchQueue.main.async {
messageTableView.reloadData()
}
如果还是不行,你可以在上面的代码中再添加一件事。假设您的 class 中有一组消息。 var messages
那么你就可以使用这段代码在tableview中正确显示数据了。
DispatchQueue.main.async {
messageTableView.reloadData()
if messages.count > 0 {
let indexPath = IndexPath(row: messages.count - 1, section: 0)
self.messageTableView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
希望这对您有所帮助。编码愉快。