1
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user