From 1d6371c37ee9d0fb277a65d8a70f18664d064ab1 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Wed, 3 Dec 2025 18:53:15 +0800 Subject: [PATCH] 1 --- keyBoard/Class/Home/V/HomeHotCell.h | 12 +++++-- keyBoard/Class/Home/V/HomeHotCell.m | 56 ++++++++++++++++++++++++----- keyBoard/Class/Home/VC/HomeHotVC.m | 11 ++---- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/keyBoard/Class/Home/V/HomeHotCell.h b/keyBoard/Class/Home/V/HomeHotCell.h index 8884126..f37f6b8 100644 --- a/keyBoard/Class/Home/V/HomeHotCell.h +++ b/keyBoard/Class/Home/V/HomeHotCell.h @@ -6,15 +6,21 @@ // #import "BaseCell.h" +#import "KBCharacter.h" NS_ASSUME_NONNULL_BEGIN @interface HomeHotCell : BaseCell -/// 配置数据 -- (void)configWithRank:(NSInteger)rank title:(NSString *)title subtitle:(NSString *)sub joined:(BOOL)joined; +/// 当前展示的排行榜角色模型 +@property (nonatomic, strong, nullable) KBCharacter *character; + +/// 旧的配置方法(不再推荐使用),内部会转成 character 赋值 +- (void)configWithRank:(NSInteger)rank + title:(NSString *)title + subtitle:(NSString *)sub + joined:(BOOL)joined; @end NS_ASSUME_NONNULL_END - diff --git a/keyBoard/Class/Home/V/HomeHotCell.m b/keyBoard/Class/Home/V/HomeHotCell.m index a3ee0b6..dd4be1e 100644 --- a/keyBoard/Class/Home/V/HomeHotCell.m +++ b/keyBoard/Class/Home/V/HomeHotCell.m @@ -4,13 +4,14 @@ // #import "HomeHotCell.h" +#import "UIImageView+KBWebImage.h" @interface HomeHotCell() // 左侧序号 @property (nonatomic, strong) UILabel *rankLabel; @property (nonatomic, strong) UIView *yuanView; -// 左侧圆形头像占位 -@property (nonatomic, strong) UIView *avatarView; +// 左侧圆形头像 +@property (nonatomic, strong) UIImageView *avatarView; // 标题 @property (nonatomic, strong) UILabel *titleLabel; // 副标题 @@ -69,11 +70,31 @@ }]; } -- (void)configWithRank:(NSInteger)rank title:(NSString *)title subtitle:(NSString *)sub joined:(BOOL)joined { - self.rankLabel.text = [NSString stringWithFormat:@"%ld", (long)rank]; - self.titleLabel.text = title ?: @""; - self.subLabel.text = sub ?: @""; +- (void)setCharacter:(KBCharacter *)character { + _character = character; + // 基本文案 + NSInteger rank = character.rank; + self.rankLabel.text = rank > 0 ? [NSString stringWithFormat:@"%ld", (long)rank] : @""; + self.titleLabel.text = character.characterName ?: @""; + self.subLabel.text = character.download ?: @""; + + // 头像:圆形占位 + 网络图 + UIImage *placeholder = nil; + if (!placeholder) { + // 使用纯色圆形作为占位 + CGFloat side = 56.0; + UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), NO, 0); + CGContextRef ctx = UIGraphicsGetCurrentContext(); + [[UIColor colorWithWhite:0.9 alpha:1.0] setFill]; + CGContextFillEllipseInRect(ctx, CGRectMake(0, 0, side, side)); + placeholder = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + } + [self.avatarView kb_setAvatarURL:character.avatarUrl placeholder:placeholder]; + + // 加入状态 + BOOL joined = character.added; if (joined) { // 已加入状态:灰背景、打勾 [self.actionButton setTitle:@"✓" forState:UIControlStateNormal]; @@ -87,6 +108,23 @@ } } +// 兼容旧调用:转成一个临时模型再走统一逻辑 +- (void)configWithRank:(NSInteger)rank title:(NSString *)title subtitle:(NSString *)sub joined:(BOOL)joined { + KBCharacter *c = [KBCharacter new]; + c.rank = rank; + c.characterName = title; + c.download = sub; + c.added = joined; + self.character = c; +} + +- (void)prepareForReuse { + [super prepareForReuse]; + [self.avatarView kb_cancelImageLoad]; + self.avatarView.image = nil; + self.character = nil; +} + #pragma mark - Lazy - (UILabel *)rankLabel { @@ -98,12 +136,13 @@ return _rankLabel; } -- (UIView *)avatarView { +- (UIImageView *)avatarView { if (!_avatarView) { - _avatarView = [[UIView alloc] init]; + _avatarView = [[UIImageView alloc] init]; _avatarView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1]; _avatarView.layer.cornerRadius = 28; _avatarView.layer.masksToBounds = YES; + _avatarView.contentMode = UIViewContentModeScaleAspectFill; } return _avatarView; } @@ -148,4 +187,3 @@ } @end - diff --git a/keyBoard/Class/Home/VC/HomeHotVC.m b/keyBoard/Class/Home/VC/HomeHotVC.m index 5b31a3b..7cc3f08 100644 --- a/keyBoard/Class/Home/VC/HomeHotVC.m +++ b/keyBoard/Class/Home/VC/HomeHotVC.m @@ -118,15 +118,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { HomeHotCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeHotCell.reuseId forIndexPath:indexPath]; KBCharacter *item = self.listCharacters[indexPath.row]; - NSInteger rank = (item.rank > 0) ? item.rank : (indexPath.row + 4); // 兜底:接口没给 rank 时,从 4 开始递增 - NSString *title = item.characterName ?: @""; - NSString *sub = item.download ?: @""; // 暂用 download 字段作为副标题展示 - BOOL joined = item.added; - // 配置 cell - [cell configWithRank:rank - title:title - subtitle:sub - joined:joined]; + // 直接把模型交给 cell,由 cell 自己负责展示 + cell.character = item; return cell; }