修复聊天最后一条跑到最上面的问题

This commit is contained in:
2026-01-29 13:13:42 +08:00
parent ef52cd4872
commit 4392296616
3 changed files with 126 additions and 59 deletions

View File

@@ -177,19 +177,17 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.currentPage = 1;
self.hasMoreHistory = YES;
// messages
NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
if (cachedMessages.count > 0) {
// 使
self.messages = [cachedMessages mutableCopy];
self.hasLoadedData = YES;
NSLog(@"[Cell] ✅ 从缓存加载personaId=%ld, 消息数=%ld", (long)persona.personaId, (long)cachedMessages.count);
} else {
//
//
// NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
// if (cachedMessages.count > 0) {
// self.messages = [cachedMessages mutableCopy];
// self.hasLoadedData = YES;
// NSLog(@"[Cell] ✅ 从缓存加载personaId=%ld, 消息数=%ld", (long)persona.personaId, (long)cachedMessages.count);
// } else {
self.messages = [NSMutableArray array];
self.hasLoadedData = NO;
NSLog(@"[Cell] ⚠️ 缓存未命中personaId=%ld, 需要请求数据", (long)persona.personaId);
}
NSLog(@"[Cell] ⚠️ 缓存已禁用personaId=%ld, 需要请求数据", (long)persona.personaId);
// }
// UI
[self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:persona.coverImageUrl]
@@ -205,6 +203,12 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
//
[self ensureOpeningMessageAtTop];
NSLog(@"[KBPersonaChatCell] ========== setPersona 调试 ==========");
NSLog(@"[KBPersonaChatCell] personaId: %ld", (long)persona.personaId);
NSLog(@"[KBPersonaChatCell] messages.count: %ld", (long)self.messages.count);
NSLog(@"[KBPersonaChatCell] chatView.frame: %@", NSStringFromCGRect(self.chatView.frame));
NSLog(@"[KBPersonaChatCell] contentView.frame: %@", NSStringFromCGRect(self.contentView.frame));
//
if (self.messages.count > 0) {
//
@@ -217,6 +221,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
[self.chatView clearMessages];
}
NSLog(@"[KBPersonaChatCell] ========== setPersona 结束 ==========");
[self.commentButton setTitle:persona.commentCount forState:UIControlStateNormal];
[self.likeButton setTitle:persona.likeCount forState:UIControlStateNormal];
self.likeButton.selected = persona.liked;
@@ -294,7 +300,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
//
if (weakSelf.currentPage == 1) {
// dispatch_async currentPage
NSInteger loadedPage = weakSelf.currentPage;
if (loadedPage == 1) {
//
weakSelf.messages = newMessages;
[weakSelf ensureOpeningMessageAtTop];
@@ -310,15 +319,26 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
// UI
dispatch_async(dispatch_get_main_queue(), ^{
BOOL keepOffset = (weakSelf.currentPage != 1);
BOOL scrollToBottom = (weakSelf.currentPage == 1);
[weakSelf.chatView reloadWithMessages:weakSelf.messages
keepOffset:keepOffset
scrollToBottom:scrollToBottom];
[weakSelf.chatView endLoadMoreWithHasMoreData:weakSelf.hasMoreHistory];
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
NSLog(@"[KBPersonaChatCell] ⚠️ strongSelf 为空,跳过刷新");
return;
}
// 使 currentPage
BOOL keepOffset = (loadedPage != 1);
BOOL scrollToBottom = (loadedPage == 1);
NSLog(@"[KBPersonaChatCell] 刷新 UI - loadedPage: %ld, keepOffset: %d, scrollToBottom: %d",
(long)loadedPage, keepOffset, scrollToBottom);
[strongSelf.chatView reloadWithMessages:strongSelf.messages
keepOffset:keepOffset
scrollToBottom:scrollToBottom];
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
//
[[KBAIChatMessageCacheManager shared] saveMessages:weakSelf.messages
[[KBAIChatMessageCacheManager shared] saveMessages:strongSelf.messages
forCompanionId:companionId];
});