diff --git a/keyBoard/Class/AiTalk/V/KBAICommentFooterView.m b/keyBoard/Class/AiTalk/V/KBAICommentFooterView.m index 4cffbab..f58d59f 100644 --- a/keyBoard/Class/AiTalk/V/KBAICommentFooterView.m +++ b/keyBoard/Class/AiTalk/V/KBAICommentFooterView.m @@ -69,8 +69,10 @@ } case KBAIReplyFooterStateLoadMore: { self.actionButton.hidden = NO; - NSInteger remaining = comment.totalReplyCount - comment.displayedReplies.count; - title = [NSString stringWithFormat:@"展开更多回复(%ld条)", (long)remaining]; + NSInteger remaining = + comment.totalReplyCount - comment.displayedReplies.count; + title = + [NSString stringWithFormat:@"展开更多回复(%ld条)", (long)remaining]; [self.actionButton setImage:[UIImage systemImageNamed:@"chevron.down"] forState:UIControlStateNormal]; break; @@ -108,8 +110,8 @@ // 文字在左,图标在右 _actionButton.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight; - _actionButton.imageEdgeInsets = UIEdgeInsetsMake(0, 4, 0, -4); - _actionButton.titleEdgeInsets = UIEdgeInsetsMake(0, -4, 0, 4); + _actionButton.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, 2); + _actionButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, -2); [_actionButton addTarget:self action:@selector(actionButtonTapped) diff --git a/keyBoard/Class/AiTalk/V/KBAICommentInputView.h b/keyBoard/Class/AiTalk/V/KBAICommentInputView.h index d4d5368..54df811 100644 --- a/keyBoard/Class/AiTalk/V/KBAICommentInputView.h +++ b/keyBoard/Class/AiTalk/V/KBAICommentInputView.h @@ -21,6 +21,9 @@ NS_ASSUME_NONNULL_BEGIN /// 清空输入框 - (void)clearText; +/// 弹起键盘 +- (void)showKeyboard; + @end NS_ASSUME_NONNULL_END diff --git a/keyBoard/Class/AiTalk/V/KBAICommentInputView.m b/keyBoard/Class/AiTalk/V/KBAICommentInputView.m index ade0f06..a4d7edc 100644 --- a/keyBoard/Class/AiTalk/V/KBAICommentInputView.m +++ b/keyBoard/Class/AiTalk/V/KBAICommentInputView.m @@ -83,6 +83,10 @@ [self updateSendButtonState]; } +- (void)showKeyboard { + [self.textField becomeFirstResponder]; +} + #pragma mark - Actions - (void)sendButtonTapped { diff --git a/keyBoard/Class/AiTalk/V/KBAICommentView.m b/keyBoard/Class/AiTalk/V/KBAICommentView.m index e81be8a..e3b3326 100644 --- a/keyBoard/Class/AiTalk/V/KBAICommentView.m +++ b/keyBoard/Class/AiTalk/V/KBAICommentView.m @@ -93,7 +93,8 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter"; [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self); make.height.mas_equalTo(50); - self.inputBottomConstraint = make.bottom.equalTo(self).offset(-KB_SafeAreaBottom()); + self.inputBottomConstraint = + make.bottom.equalTo(self).offset(-KB_SafeAreaBottom()); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { @@ -148,7 +149,8 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter"; [self.inputBottomConstraint uninstall]; [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) { - self.inputBottomConstraint = make.bottom.equalTo(self); + self.inputBottomConstraint = + make.bottom.equalTo(self).offset(-KB_SafeAreaBottom()); }]; [UIView animateWithDuration:duration @@ -186,20 +188,22 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter"; NSArray *commentsArray = json[@"comments"]; [self.comments removeAllObjects]; - + // 获取 tableView 宽度用于计算高度 CGFloat tableWidth = self.tableView.bounds.size.width; if (tableWidth <= 0) { tableWidth = [UIScreen mainScreen].bounds.size.width; } - + for (NSDictionary *dict in commentsArray) { KBAICommentModel *comment = [KBAICommentModel mj_objectWithKeyValues:dict]; // 预先计算并缓存 Header 高度 - comment.cachedHeaderHeight = [comment calculateHeaderHeightWithMaxWidth:tableWidth]; + comment.cachedHeaderHeight = + [comment calculateHeaderHeightWithMaxWidth:tableWidth]; // 预先计算并缓存所有 Reply 高度 for (KBAIReplyModel *reply in comment.replies) { - reply.cachedCellHeight = [reply calculateCellHeightWithMaxWidth:tableWidth]; + reply.cachedCellHeight = + [reply calculateCellHeightWithMaxWidth:tableWidth]; } [self.comments addObject:comment]; } @@ -250,7 +254,7 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter"; [weakSelf.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone]; }; - + cell.onReplyAction = ^{ [weakSelf setReplyToComment:comment reply:reply]; }; @@ -276,7 +280,7 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter"; [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone]; }; - + header.onReplyAction = ^{ [weakSelf setReplyToComment:comment reply:nil]; }; @@ -357,17 +361,18 @@ static NSInteger const kRepliesLoadCount = 5; - (void)loadMoreRepliesForSection:(NSInteger)section { KBAICommentModel *comment = self.comments[section]; NSInteger currentCount = comment.displayedReplies.count; - + // 加载更多回复 [comment loadMoreReplies:kRepliesLoadCount]; - + // 计算新增的行 NSInteger newCount = comment.displayedReplies.count; NSMutableArray *insertIndexPaths = [NSMutableArray array]; for (NSInteger i = currentCount; i < newCount; i++) { - [insertIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]]; + [insertIndexPaths addObject:[NSIndexPath indexPathForRow:i + inSection:section]]; } - + // 插入行(不刷新 Header,避免头像闪烁) [self.tableView beginUpdates]; if (insertIndexPaths.count > 0) { @@ -375,7 +380,7 @@ static NSInteger const kRepliesLoadCount = 5; withRowAnimation:UITableViewRowAnimationAutomatic]; } [self.tableView endUpdates]; - + // 手动刷新 Footer KBAICommentFooterView *footerView = (KBAICommentFooterView *)[self.tableView footerViewForSection:section]; @@ -387,16 +392,17 @@ static NSInteger const kRepliesLoadCount = 5; - (void)collapseRepliesForSection:(NSInteger)section { KBAICommentModel *comment = self.comments[section]; NSInteger rowCount = comment.displayedReplies.count; - + // 计算要删除的行 NSMutableArray *deleteIndexPaths = [NSMutableArray array]; for (NSInteger i = 0; i < rowCount; i++) { - [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]]; + [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i + inSection:section]]; } - + // 收起全部回复 [comment collapseReplies]; - + // 删除行(不刷新 Header,避免头像闪烁) [self.tableView beginUpdates]; if (deleteIndexPaths.count > 0) { @@ -404,7 +410,7 @@ static NSInteger const kRepliesLoadCount = 5; withRowAnimation:UITableViewRowAnimationAutomatic]; } [self.tableView endUpdates]; - + // 手动刷新 Footer KBAICommentFooterView *footerView = (KBAICommentFooterView *)[self.tableView footerViewForSection:section]; @@ -458,7 +464,7 @@ static NSInteger const kRepliesLoadCount = 5; - (BaseTableView *)tableView { if (!_tableView) { _tableView = [[BaseTableView alloc] initWithFrame:CGRectZero - style:UITableViewStyleGrouped]; + style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.backgroundColor = [UIColor whiteColor]; @@ -496,20 +502,26 @@ static NSInteger const kRepliesLoadCount = 5; #pragma mark - Reply -- (void)setReplyToComment:(KBAICommentModel *)comment reply:(KBAIReplyModel *)reply { +- (void)setReplyToComment:(KBAICommentModel *)comment + reply:(KBAIReplyModel *)reply { self.replyToComment = comment; self.replyToReply = reply; - + if (reply) { // 回复二级评论 - self.inputView.placeholder = [NSString stringWithFormat:@"回复 @%@", reply.userName]; + self.inputView.placeholder = + [NSString stringWithFormat:@"回复 @%@", reply.userName]; } else if (comment) { // 回复一级评论 - self.inputView.placeholder = [NSString stringWithFormat:@"回复 @%@", comment.userName]; + self.inputView.placeholder = + [NSString stringWithFormat:@"回复 @%@", comment.userName]; } else { // 普通评论 self.inputView.placeholder = @"说点什么..."; } + + // 弹起键盘 + [self.inputView showKeyboard]; } - (void)clearReplyTarget { @@ -521,13 +533,14 @@ static NSInteger const kRepliesLoadCount = 5; #pragma mark - Send Comment - (void)sendCommentWithText:(NSString *)text { - if (text.length == 0) return; - + if (text.length == 0) + return; + CGFloat tableWidth = self.tableView.bounds.size.width; if (tableWidth <= 0) { tableWidth = [UIScreen mainScreen].bounds.size.width; } - + if (self.replyToComment) { // 回复评论(添加二级评论) [self sendReplyWithText:text tableWidth:tableWidth]; @@ -535,7 +548,7 @@ static NSInteger const kRepliesLoadCount = 5; // 发送一级评论 [self sendNewCommentWithText:text tableWidth:tableWidth]; } - + // 清空输入框和回复目标 [self.inputView clearText]; [self clearReplyTarget]; @@ -553,27 +566,29 @@ static NSInteger const kRepliesLoadCount = 5; newComment.isLiked = NO; newComment.createTime = [[NSDate date] timeIntervalSince1970]; newComment.replies = @[]; - + // 计算高度缓存 - newComment.cachedHeaderHeight = [newComment calculateHeaderHeightWithMaxWidth:tableWidth]; - + newComment.cachedHeaderHeight = + [newComment calculateHeaderHeightWithMaxWidth:tableWidth]; + // 插入到数组第一个 [self.comments insertObject:newComment atIndex:0]; self.totalCommentCount++; [self updateTitle]; - + // 插入新 section [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; - + // 滚动到顶部 [self.tableView setContentOffset:CGPointZero animated:YES]; } - (void)sendReplyWithText:(NSString *)text tableWidth:(CGFloat)tableWidth { KBAICommentModel *comment = self.replyToComment; - if (!comment) return; - + if (!comment) + return; + // 创建新二级评论 KBAIReplyModel *newReply = [[KBAIReplyModel alloc] init]; newReply.replyId = [NSUUID UUID].UUIDString; @@ -584,41 +599,44 @@ static NSInteger const kRepliesLoadCount = 5; newReply.likeCount = 0; newReply.isLiked = NO; newReply.createTime = [[NSDate date] timeIntervalSince1970]; - + // 如果是回复二级评论,设置被回复用户 if (self.replyToReply) { newReply.replyToUserName = self.replyToReply.userName; } - + // 计算高度缓存 - newReply.cachedCellHeight = [newReply calculateCellHeightWithMaxWidth:tableWidth]; - + newReply.cachedCellHeight = + [newReply calculateCellHeightWithMaxWidth:tableWidth]; + // 添加到 replies 数组 NSMutableArray *newReplies = [NSMutableArray arrayWithArray:comment.replies]; [newReplies addObject:newReply]; comment.replies = newReplies; comment.totalReplyCount = newReplies.count; - + // 找到该评论的 section NSInteger section = [self.comments indexOfObject:comment]; - if (section == NSNotFound) return; - + if (section == NSNotFound) + return; + // 如果已展开,添加到 displayedReplies 并插入行 if (comment.isRepliesExpanded) { NSInteger newRowIndex = comment.displayedReplies.count; [comment.displayedReplies addObject:newReply]; - - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:section]; - [self.tableView insertRowsAtIndexPaths:@[indexPath] + + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex + inSection:section]; + [self.tableView insertRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; - + // 刷新 Footer KBAICommentFooterView *footerView = (KBAICommentFooterView *)[self.tableView footerViewForSection:section]; if (footerView) { [footerView configureWithComment:comment]; } - + // 滚动到新回复 [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom