添加二级评论多个的时候的逻辑,默认每次点击出现5条

This commit is contained in:
2026-01-16 19:29:42 +08:00
parent 7fa124d45f
commit ac0d9584d8
5 changed files with 57 additions and 21 deletions

View File

@@ -15,7 +15,8 @@ NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, KBAIReplyFooterState) {
KBAIReplyFooterStateHidden, // 无二级评论,不显示
KBAIReplyFooterStateExpand, // 显示"展开x条回复"(折叠状态)
KBAIReplyFooterStateCollapse // 显示"收起"(展开状态
KBAIReplyFooterStateLoadMore, // 显示"展开更多回复"(部分展开
KBAIReplyFooterStateCollapse // 显示"收起"(全部展开)
};
/// 一级评论模型
@@ -75,8 +76,9 @@ typedef NS_ENUM(NSInteger, KBAIReplyFooterState) {
/// 获取当前 Footer 状态
- (KBAIReplyFooterState)footerState;
/// 展开全部回复
- (void)expandAllReplies;
/// 加载更多回复(每次加载指定数量)
/// @param count 本次加载的数量
- (void)loadMoreReplies:(NSInteger)count;
/// 收起所有回复
- (void)collapseReplies;

View File

@@ -64,19 +64,29 @@
return KBAIReplyFooterStateHidden;
}
//
if (self.isRepliesExpanded) {
return KBAIReplyFooterStateCollapse;
//
if (!self.isRepliesExpanded) {
return KBAIReplyFooterStateExpand;
}
//
return KBAIReplyFooterStateExpand;
//
if (self.displayedReplies.count < self.totalReplyCount) {
return KBAIReplyFooterStateLoadMore;
}
//
return KBAIReplyFooterStateCollapse;
}
- (void)expandAllReplies {
- (void)loadMoreReplies:(NSInteger)count {
self.isRepliesExpanded = YES;
[self.displayedReplies removeAllObjects];
[self.displayedReplies addObjectsFromArray:self.replies];
NSInteger currentCount = self.displayedReplies.count;
NSInteger endIndex = MIN(currentCount + count, self.replies.count);
for (NSInteger i = currentCount; i < endIndex; i++) {
[self.displayedReplies addObject:self.replies[i]];
}
}
- (void)collapseReplies {

View File

@@ -15,7 +15,10 @@
{"id": "reply_001_2", "userId": "user_103", "userName": "程序员小李", "avatarUrl": "https://picsum.photos/100/100?random=3", "content": "感谢支持,会继续努力的!", "replyToUserName": "科技达人", "likeCount": 23, "isLiked": true, "createTime": 1737018000},
{"id": "reply_001_3", "userId": "user_104", "userName": "产品经理", "avatarUrl": "https://picsum.photos/100/100?random=4", "content": "下个版本会有更多惊喜", "likeCount": 12, "isLiked": false, "createTime": 1737021600},
{"id": "reply_001_4", "userId": "user_105", "userName": "UI设计师", "avatarUrl": "https://picsum.photos/100/100?random=5", "content": "细节决定品质", "likeCount": 8, "isLiked": false, "createTime": 1737025200},
{"id": "reply_001_5", "userId": "user_106", "userName": "测试工程师", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 5, "isLiked": false, "createTime": 1737028800}
{"id": "reply_001_5", "userId": "user_106", "userName": "测试工程师", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 5, "isLiked": false, "createTime": 1737028800},
{"id": "reply_001_6", "userId": "user_106", "userName": "测试工程师2", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 51, "isLiked": false, "createTime": 1737028800},
{"id": "reply_001_7", "userId": "user_106", "userName": "测试工程师3", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 52, "isLiked": false, "createTime": 1737028800},
{"id": "reply_001_8", "userId": "user_106", "userName": "测试工程师4", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 53, "isLiked": false, "createTime": 1737028800}
]
},
{
@@ -43,8 +46,15 @@
"isLiked": false,
"createTime": 1736751600,
"replies": [
{"id": "reply_004_1", "userId": "user_402", "userName": "新用户", "avatarUrl": "https://picsum.photos/100/100?random=14", "content": "真的吗?那我也要试试", "likeCount": 18, "isLiked": false, "createTime": 1736755200}
]
{"id": "reply_004_1", "userId": "user_102", "userName": "科技达人", "avatarUrl": "https://picsum.photos/100/100?random=2", "content": "同意!这个设计真的很用心", "likeCount": 45, "isLiked": false, "createTime": 1737014400},
{"id": "reply_004_2", "userId": "user_103", "userName": "程序员小李", "avatarUrl": "https://picsum.photos/100/100?random=3", "content": "感谢支持,会继续努力的!", "replyToUserName": "科技达人", "likeCount": 23, "isLiked": true, "createTime": 1737018000},
{"id": "reply_004_3", "userId": "user_104", "userName": "产品经理", "avatarUrl": "https://picsum.photos/100/100?random=4", "content": "下个版本会有更多惊喜", "likeCount": 12, "isLiked": false, "createTime": 1737021600},
{"id": "reply_004_4", "userId": "user_105", "userName": "UI设计师", "avatarUrl": "https://picsum.photos/100/100?random=5", "content": "细节决定品质", "likeCount": 8, "isLiked": false, "createTime": 1737025200},
{"id": "reply_004_5", "userId": "user_106", "userName": "测试工程师", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 5, "isLiked": false, "createTime": 1737028800},
{"id": "reply_004_6", "userId": "user_106", "userName": "测试工程师2", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 51, "isLiked": false, "createTime": 1737028800},
{"id": "reply_004_7", "userId": "user_106", "userName": "测试工程师3", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 52, "isLiked": false, "createTime": 1737028800},
{"id": "reply_004_8", "userId": "user_106", "userName": "测试工程师4", "avatarUrl": "https://picsum.photos/100/100?random=6", "content": "已经测试通过了,稳定性很好", "likeCount": 53, "isLiked": false, "createTime": 1737028800}
]
},
{
"id": "comment_005",

View File

@@ -67,6 +67,14 @@
forState:UIControlStateNormal];
break;
}
case KBAIReplyFooterStateLoadMore: {
self.actionButton.hidden = NO;
NSInteger remaining = comment.totalReplyCount - comment.displayedReplies.count;
title = [NSString stringWithFormat:@"展开更多回复(%ld条", (long)remaining];
[self.actionButton setImage:[UIImage systemImageNamed:@"chevron.down"]
forState:UIControlStateNormal];
break;
}
case KBAIReplyFooterStateCollapse: {
self.actionButton.hidden = NO;
title = @"收起";

View File

@@ -319,13 +319,17 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
#pragma mark - Footer Actions
///
static NSInteger const kRepliesLoadCount = 5;
- (void)handleFooterActionForSection:(NSInteger)section {
KBAICommentModel *comment = self.comments[section];
KBAIReplyFooterState state = [comment footerState];
switch (state) {
case KBAIReplyFooterStateExpand: {
[self expandRepliesForSection:section];
case KBAIReplyFooterStateExpand:
case KBAIReplyFooterStateLoadMore: {
[self loadMoreRepliesForSection:section];
break;
}
case KBAIReplyFooterStateCollapse: {
@@ -337,15 +341,17 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
}
}
- (void)expandRepliesForSection:(NSInteger)section {
- (void)loadMoreRepliesForSection:(NSInteger)section {
KBAICommentModel *comment = self.comments[section];
NSInteger currentCount = comment.displayedReplies.count;
//
[comment expandAllReplies];
//
[comment loadMoreReplies:kRepliesLoadCount];
//
//
NSInteger newCount = comment.displayedReplies.count;
NSMutableArray *insertIndexPaths = [NSMutableArray array];
for (NSInteger i = 0; i < comment.displayedReplies.count; i++) {
for (NSInteger i = currentCount; i < newCount; i++) {
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}