From 7a25a6a5fa634bfaf1b699036d9fd5bb9922ee5d Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Thu, 4 Dec 2025 19:24:42 +0800 Subject: [PATCH] 1 --- keyBoard/Class/Home/VC/HomeRankContentVC.m | 38 ++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/keyBoard/Class/Home/VC/HomeRankContentVC.m b/keyBoard/Class/Home/VC/HomeRankContentVC.m index d338451..3de6104 100644 --- a/keyBoard/Class/Home/VC/HomeRankContentVC.m +++ b/keyBoard/Class/Home/VC/HomeRankContentVC.m @@ -132,13 +132,39 @@ // 直接把模型交给 cell,由 cell 自己负责展示 cell.character = c; KBWeakSelf + __weak typeof(cell) weakCell = cell; cell.onTapAction = ^{ - // 切换添加/已添加状态并刷新该项(只是本地 UI 状态) - NSMutableArray *m = [weakSelf.characters mutableCopy]; - KBCharacter *mc = m[indexPath.item]; - mc.added = !mc.added; - weakSelf.characters = [m copy]; - [weakSelf.collectionView reloadItemsAtIndexPaths:@[indexPath]]; + __strong typeof(weakSelf) self = weakSelf; + HomeRankCardCell *strongCell = weakCell; + if (!self || !strongCell) { return; } + + NSIndexPath *currentIndexPath = [collectionView indexPathForCell:strongCell]; + if (!currentIndexPath) { return; } + if (currentIndexPath.item >= self.characters.count) { return; } + + KBCharacter *mc = self.characters[currentIndexPath.item]; + // 已添加状态下不允许再次点击 + if (mc.added) { return; } + + NSString *cidStr = mc.ID ?: @""; + if (cidStr.length == 0) { return; } + NSNumber *cid = @([cidStr integerValue]); + + [self.homeVM addUserCharacterWithId:cid + completion:^(BOOL success, NSError * _Nullable error) { + if (!success) { + NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error"); + [KBHUD showInfo:msg]; + return; + } + + // 添加成功:更新模型状态并刷新该 item(按钮变为已添加且不可再点) + mc.added = YES; + NSMutableArray *m = [self.characters mutableCopy]; + [m replaceObjectAtIndex:currentIndexPath.item withObject:mc]; + self.characters = [m copy]; + [self.collectionView reloadItemsAtIndexPaths:@[currentIndexPath]]; + }]; }; return cell; }