修复聊天最后一条跑到最上面的问题
This commit is contained in:
@@ -90,10 +90,13 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
make.edges.equalTo(self);
|
||||
}];
|
||||
|
||||
// 关键修复:减少初始 contentBottomInset,因为 chatView 已经通过约束避开了底部
|
||||
self.contentBottomInset = 20; // 简单的缓冲空间
|
||||
// 初始化 contentInset
|
||||
self.contentBottomInset = 0;
|
||||
[self updateContentBottomInset:self.contentBottomInset];
|
||||
|
||||
|
||||
// 暂时禁用 mj_footer,排查问题
|
||||
// TODO: 如果需要加载更多功能,重新启用
|
||||
/*
|
||||
__weak typeof(self) weakSelf = self;
|
||||
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
@@ -114,11 +117,12 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
}];
|
||||
|
||||
// 隐藏"已经全部加载完毕"的提示文字
|
||||
MJRefreshAutoNormalFooter *footer = (MJRefreshAutoNormalFooter *)self.tableView.mj_footer;
|
||||
footer.stateLabel.hidden = YES; // 隐藏状态文字
|
||||
footer.refreshingBlock = footer.refreshingBlock; // 保持刷新逻辑
|
||||
// MJRefreshAutoNormalFooter *footer = (MJRefreshAutoNormalFooter *)self.tableView.mj_footer;
|
||||
// footer.stateLabel.hidden = YES; // 隐藏状态文字
|
||||
// footer.refreshingBlock = footer.refreshingBlock; // 保持刷新逻辑
|
||||
|
||||
self.tableView.mj_footer.hidden = YES;
|
||||
// self.tableView.mj_footer.hidden = YES;
|
||||
*/
|
||||
}
|
||||
|
||||
#pragma mark - Public Methods
|
||||
@@ -206,39 +210,56 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
// 关键修复:使用 layoutIfNeeded 确保布局完成后再滚动
|
||||
[self.tableView layoutIfNeeded];
|
||||
|
||||
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:self.messages.count - 1
|
||||
inSection:0];
|
||||
// 计算需要滚动到的位置
|
||||
CGFloat contentHeight = self.tableView.contentSize.height;
|
||||
CGFloat tableViewHeight = self.tableView.bounds.size.height;
|
||||
CGFloat bottomInset = self.tableView.contentInset.bottom;
|
||||
|
||||
// 直接滚动到最后一条消息,不检查是否可见(确保新消息能被看到)
|
||||
[self.tableView scrollToRowAtIndexPath:lastIndexPath
|
||||
atScrollPosition:UITableViewScrollPositionBottom
|
||||
animated:animated];
|
||||
// 如果内容高度小于 tableView 高度,不需要滚动
|
||||
if (contentHeight <= tableViewHeight) {
|
||||
NSLog(@"[KBChatTableView] 内容高度(%.2f) <= tableView高度(%.2f),不需要滚动", contentHeight, tableViewHeight);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算滚动到底部的 offset
|
||||
CGFloat offsetY = contentHeight - tableViewHeight + bottomInset;
|
||||
|
||||
NSLog(@"[KBChatTableView] scrollToBottom - contentHeight: %.2f, tableViewHeight: %.2f, bottomInset: %.2f, offsetY: %.2f",
|
||||
contentHeight, tableViewHeight, bottomInset, offsetY);
|
||||
|
||||
[self.tableView setContentOffset:CGPointMake(0, offsetY) animated:animated];
|
||||
}
|
||||
|
||||
#pragma mark - Public Helpers
|
||||
|
||||
- (void)endLoadMoreWithHasMoreData:(BOOL)hasMoreData {
|
||||
self.hasMoreData = hasMoreData;
|
||||
if (hasMoreData) {
|
||||
[self.tableView.mj_footer endRefreshing];
|
||||
} else {
|
||||
[self.tableView.mj_footer endRefreshingWithNoMoreData];
|
||||
}
|
||||
// 暂时禁用 mj_footer
|
||||
// if (hasMoreData) {
|
||||
// [self.tableView.mj_footer endRefreshing];
|
||||
// } else {
|
||||
// [self.tableView.mj_footer endRefreshingWithNoMoreData];
|
||||
// }
|
||||
[self updateFooterVisibility];
|
||||
}
|
||||
|
||||
- (void)resetNoMoreData {
|
||||
self.hasMoreData = YES;
|
||||
[self.tableView.mj_footer resetNoMoreData];
|
||||
// 暂时禁用 mj_footer
|
||||
// [self.tableView.mj_footer resetNoMoreData];
|
||||
[self updateFooterVisibility];
|
||||
}
|
||||
|
||||
- (void)updateContentBottomInset:(CGFloat)bottomInset {
|
||||
self.contentBottomInset = bottomInset;
|
||||
UIEdgeInsets insets = self.tableView.contentInset;
|
||||
|
||||
// 直接设置 contentInset
|
||||
UIEdgeInsets insets = UIEdgeInsetsZero;
|
||||
insets.bottom = bottomInset;
|
||||
self.tableView.contentInset = insets;
|
||||
self.tableView.scrollIndicatorInsets = insets;
|
||||
|
||||
NSLog(@"[KBChatTableView] updateContentBottomInset: %.2f", bottomInset);
|
||||
}
|
||||
|
||||
- (void)addMessage:(KBAiChatMessage *)message
|
||||
@@ -296,10 +317,32 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
}
|
||||
}
|
||||
|
||||
NSLog(@"[KBChatTableView] ========== reloadWithMessages 开始 ==========");
|
||||
NSLog(@"[KBChatTableView] 消息数量: %ld", (long)self.messages.count);
|
||||
NSLog(@"[KBChatTableView] tableView.frame: %@", NSStringFromCGRect(self.tableView.frame));
|
||||
NSLog(@"[KBChatTableView] tableView.bounds: %@", NSStringFromCGRect(self.tableView.bounds));
|
||||
NSLog(@"[KBChatTableView] 刷新前 contentSize: %@", NSStringFromCGSize(self.tableView.contentSize));
|
||||
NSLog(@"[KBChatTableView] 刷新前 contentInset: %@", NSStringFromUIEdgeInsets(self.tableView.contentInset));
|
||||
|
||||
[self.tableView reloadData];
|
||||
[self.tableView layoutIfNeeded];
|
||||
[self updateFooterVisibility];
|
||||
|
||||
NSLog(@"[KBChatTableView] 刷新后 contentSize: %@", NSStringFromCGSize(self.tableView.contentSize));
|
||||
NSLog(@"[KBChatTableView] 刷新后 contentInset: %@", NSStringFromUIEdgeInsets(self.tableView.contentInset));
|
||||
NSLog(@"[KBChatTableView] 刷新后 contentOffset: %@", NSStringFromCGPoint(self.tableView.contentOffset));
|
||||
|
||||
// 打印每个 Cell 的高度
|
||||
for (NSInteger i = 0; i < self.messages.count; i++) {
|
||||
CGRect cellRect = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
|
||||
KBAiChatMessage *msg = self.messages[i];
|
||||
NSLog(@"[KBChatTableView] Cell[%ld] type=%ld, height=%.2f, text=%@",
|
||||
(long)i, (long)msg.type, cellRect.size.height,
|
||||
msg.text.length > 20 ? [msg.text substringToIndex:20] : msg.text);
|
||||
}
|
||||
|
||||
NSLog(@"[KBChatTableView] ========== reloadWithMessages 结束 ==========");
|
||||
|
||||
if (keepOffset) {
|
||||
CGFloat newContentHeight = self.tableView.contentSize.height;
|
||||
CGFloat delta = newContentHeight - oldContentHeight;
|
||||
@@ -310,8 +353,21 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
}
|
||||
|
||||
if (scrollToBottom) {
|
||||
// 关键修复:直接滚动,不使用延迟
|
||||
[self scrollToBottomAnimated:NO];
|
||||
NSLog(@"[KBChatTableView] 准备滚动到底部...");
|
||||
// 关键修复:使用 dispatch_after 延迟执行,确保 reloadData 和布局完全完成
|
||||
__weak typeof(self) weakSelf = self;
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
NSLog(@"[KBChatTableView] ⚠️ strongSelf 为空,跳过滚动");
|
||||
return;
|
||||
}
|
||||
NSLog(@"[KBChatTableView] dispatch_after 执行 scrollToBottom");
|
||||
NSLog(@"[KBChatTableView] 滚动前 contentSize: %@", NSStringFromCGSize(strongSelf.tableView.contentSize));
|
||||
NSLog(@"[KBChatTableView] 滚动前 tableView.frame: %@", NSStringFromCGRect(strongSelf.tableView.frame));
|
||||
[strongSelf scrollToBottomAnimated:NO];
|
||||
NSLog(@"[KBChatTableView] scrollToBottom 后 contentOffset: %@", NSStringFromCGPoint(strongSelf.tableView.contentOffset));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,9 +456,10 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
}
|
||||
|
||||
- (void)updateFooterVisibility {
|
||||
BOOL canLoadMore = (self.delegate &&
|
||||
[self.delegate respondsToSelector:@selector(chatTableViewDidTriggerLoadMore:)]);
|
||||
self.tableView.mj_footer.hidden = !canLoadMore || self.messages.count == 0;
|
||||
// 暂时禁用 mj_footer
|
||||
// BOOL canLoadMore = (self.delegate &&
|
||||
// [self.delegate respondsToSelector:@selector(chatTableViewDidTriggerLoadMore:)]);
|
||||
// self.tableView.mj_footer.hidden = !canLoadMore || self.messages.count == 0;
|
||||
}
|
||||
|
||||
#pragma mark - UITableViewDataSource
|
||||
|
||||
Reference in New Issue
Block a user