处理滚动底部问题
This commit is contained in:
@@ -132,6 +132,27 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
[self addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
- (void)addLoadingUserMessage {
|
||||
KBAiChatMessage *message = [KBAiChatMessage loadingUserMessage];
|
||||
[self addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
- (void)updateLastUserMessage:(NSString *)text {
|
||||
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
||||
KBAiChatMessage *message = self.messages[i];
|
||||
if (message.type == KBAiChatMessageTypeUser && message.isLoading) {
|
||||
message.text = text;
|
||||
message.isLoading = NO;
|
||||
message.isComplete = YES;
|
||||
|
||||
// 刷新该行
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
||||
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addAssistantMessage:(NSString *)text
|
||||
audioDuration:(NSTimeInterval)duration
|
||||
audioData:(NSData *)audioData {
|
||||
@@ -198,9 +219,12 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
|
||||
KBAiChatMessage *message = self.messages[i];
|
||||
if (message.type == KBAiChatMessageTypeUser) {
|
||||
message.isLoading = NO;
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
||||
[self.tableView reloadRowsAtIndexPaths:@[indexPath]
|
||||
withRowAnimation:UITableViewRowAnimationNone];
|
||||
[self.tableView layoutIfNeeded];
|
||||
[self scrollToBottomAnimated:NO];
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -227,14 +251,9 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
CGFloat tableViewHeight = self.tableView.bounds.size.height;
|
||||
CGFloat bottomInset = self.tableView.contentInset.bottom;
|
||||
|
||||
// 如果内容高度小于 tableView 高度,不需要滚动
|
||||
if (contentHeight <= tableViewHeight) {
|
||||
NSLog(@"[KBChatTableView] 内容高度(%.2f) <= tableView高度(%.2f),不需要滚动", contentHeight, tableViewHeight);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算滚动到底部的 offset
|
||||
CGFloat offsetY = contentHeight - tableViewHeight + bottomInset;
|
||||
offsetY = MAX(0, offsetY);
|
||||
|
||||
NSLog(@"[KBChatTableView] scrollToBottom - contentHeight: %.2f, tableViewHeight: %.2f, bottomInset: %.2f, offsetY: %.2f",
|
||||
contentHeight, tableViewHeight, bottomInset, offsetY);
|
||||
@@ -312,6 +331,12 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self.tableView layoutIfNeeded]; // 再次确保布局完成
|
||||
[self scrollToBottomAnimated:YES];
|
||||
|
||||
// 二次滚动以确保底部完全可见(解决自动行高导致的布局偏差)
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self.tableView layoutIfNeeded];
|
||||
[self scrollToBottomAnimated:NO];
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user