添加二级评论多个的时候的逻辑,默认每次点击出现5条
This commit is contained in:
@@ -15,7 +15,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
typedef NS_ENUM(NSInteger, KBAIReplyFooterState) {
|
typedef NS_ENUM(NSInteger, KBAIReplyFooterState) {
|
||||||
KBAIReplyFooterStateHidden, // 无二级评论,不显示
|
KBAIReplyFooterStateHidden, // 无二级评论,不显示
|
||||||
KBAIReplyFooterStateExpand, // 显示"展开x条回复"(折叠状态)
|
KBAIReplyFooterStateExpand, // 显示"展开x条回复"(折叠状态)
|
||||||
KBAIReplyFooterStateCollapse // 显示"收起"(展开状态)
|
KBAIReplyFooterStateLoadMore, // 显示"展开更多回复"(部分展开)
|
||||||
|
KBAIReplyFooterStateCollapse // 显示"收起"(全部展开)
|
||||||
};
|
};
|
||||||
|
|
||||||
/// 一级评论模型
|
/// 一级评论模型
|
||||||
@@ -75,8 +76,9 @@ typedef NS_ENUM(NSInteger, KBAIReplyFooterState) {
|
|||||||
/// 获取当前 Footer 状态
|
/// 获取当前 Footer 状态
|
||||||
- (KBAIReplyFooterState)footerState;
|
- (KBAIReplyFooterState)footerState;
|
||||||
|
|
||||||
/// 展开全部回复
|
/// 加载更多回复(每次加载指定数量)
|
||||||
- (void)expandAllReplies;
|
/// @param count 本次加载的数量
|
||||||
|
- (void)loadMoreReplies:(NSInteger)count;
|
||||||
|
|
||||||
/// 收起所有回复
|
/// 收起所有回复
|
||||||
- (void)collapseReplies;
|
- (void)collapseReplies;
|
||||||
|
|||||||
@@ -64,19 +64,29 @@
|
|||||||
return KBAIReplyFooterStateHidden;
|
return KBAIReplyFooterStateHidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 已展开
|
// 未展开(初始状态)
|
||||||
if (self.isRepliesExpanded) {
|
if (!self.isRepliesExpanded) {
|
||||||
return KBAIReplyFooterStateCollapse;
|
return KBAIReplyFooterStateExpand;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 未展开
|
// 已展开但还有更多
|
||||||
return KBAIReplyFooterStateExpand;
|
if (self.displayedReplies.count < self.totalReplyCount) {
|
||||||
|
return KBAIReplyFooterStateLoadMore;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 全部展开
|
||||||
|
return KBAIReplyFooterStateCollapse;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)expandAllReplies {
|
- (void)loadMoreReplies:(NSInteger)count {
|
||||||
self.isRepliesExpanded = YES;
|
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 {
|
- (void)collapseReplies {
|
||||||
|
|||||||
@@ -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_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_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_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,7 +46,14 @@
|
|||||||
"isLiked": false,
|
"isLiked": false,
|
||||||
"createTime": 1736751600,
|
"createTime": 1736751600,
|
||||||
"replies": [
|
"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}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -67,6 +67,14 @@
|
|||||||
forState:UIControlStateNormal];
|
forState:UIControlStateNormal];
|
||||||
break;
|
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: {
|
case KBAIReplyFooterStateCollapse: {
|
||||||
self.actionButton.hidden = NO;
|
self.actionButton.hidden = NO;
|
||||||
title = @"收起";
|
title = @"收起";
|
||||||
|
|||||||
@@ -319,13 +319,17 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
|||||||
|
|
||||||
#pragma mark - Footer Actions
|
#pragma mark - Footer Actions
|
||||||
|
|
||||||
|
/// 每次加载的回复数量
|
||||||
|
static NSInteger const kRepliesLoadCount = 5;
|
||||||
|
|
||||||
- (void)handleFooterActionForSection:(NSInteger)section {
|
- (void)handleFooterActionForSection:(NSInteger)section {
|
||||||
KBAICommentModel *comment = self.comments[section];
|
KBAICommentModel *comment = self.comments[section];
|
||||||
KBAIReplyFooterState state = [comment footerState];
|
KBAIReplyFooterState state = [comment footerState];
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case KBAIReplyFooterStateExpand: {
|
case KBAIReplyFooterStateExpand:
|
||||||
[self expandRepliesForSection:section];
|
case KBAIReplyFooterStateLoadMore: {
|
||||||
|
[self loadMoreRepliesForSection:section];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KBAIReplyFooterStateCollapse: {
|
case KBAIReplyFooterStateCollapse: {
|
||||||
@@ -337,15 +341,17 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)expandRepliesForSection:(NSInteger)section {
|
- (void)loadMoreRepliesForSection:(NSInteger)section {
|
||||||
KBAICommentModel *comment = self.comments[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];
|
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]];
|
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user