2
This commit is contained in:
47
keyBoard/Class/AiTalk/V/KBAIMessageCell.h
Normal file
47
keyBoard/Class/AiTalk/V/KBAIMessageCell.h
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// KBAIMessageCell.h
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/28.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class KBAIMessageCell;
|
||||
|
||||
@protocol KBAIMessageCellDelegate <NSObject>
|
||||
|
||||
@optional
|
||||
/// 点击删除按钮
|
||||
- (void)messageCell:(KBAIMessageCell *)cell didTapDeleteAtIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
@end
|
||||
|
||||
@interface KBAIMessageCell : UITableViewCell
|
||||
|
||||
@property (nonatomic, weak) id<KBAIMessageCellDelegate> delegate;
|
||||
@property (nonatomic, strong) NSIndexPath *indexPath;
|
||||
|
||||
/// 头像
|
||||
@property (nonatomic, strong, readonly) UIImageView *avatarImageView;
|
||||
/// 昵称
|
||||
@property (nonatomic, strong, readonly) UILabel *nameLabel;
|
||||
/// 消息内容
|
||||
@property (nonatomic, strong, readonly) UILabel *contentLabel;
|
||||
/// 时间
|
||||
@property (nonatomic, strong, readonly) UILabel *timeLabel;
|
||||
/// 置顶图标(只显示,不做功能)
|
||||
@property (nonatomic, strong, readonly) UIImageView *pinIconView;
|
||||
|
||||
/// 配置数据
|
||||
- (void)configWithAvatar:(NSString *)avatarUrl
|
||||
name:(NSString *)name
|
||||
content:(NSString *)content
|
||||
time:(NSString *)time
|
||||
isPinned:(BOOL)isPinned;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
157
keyBoard/Class/AiTalk/V/KBAIMessageCell.m
Normal file
157
keyBoard/Class/AiTalk/V/KBAIMessageCell.m
Normal file
@@ -0,0 +1,157 @@
|
||||
//
|
||||
// KBAIMessageCell.m
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/28.
|
||||
//
|
||||
|
||||
#import "KBAIMessageCell.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
|
||||
@interface KBAIMessageCell ()
|
||||
|
||||
@property (nonatomic, strong, readwrite) UIImageView *avatarImageView;
|
||||
@property (nonatomic, strong, readwrite) UILabel *nameLabel;
|
||||
@property (nonatomic, strong, readwrite) UILabel *contentLabel;
|
||||
@property (nonatomic, strong, readwrite) UILabel *timeLabel;
|
||||
@property (nonatomic, strong, readwrite) UIImageView *pinIconView;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBAIMessageCell
|
||||
|
||||
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||||
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
|
||||
if (self) {
|
||||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||||
self.backgroundColor = [UIColor whiteColor];
|
||||
[self setupSubviews];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setupSubviews {
|
||||
[self.contentView addSubview:self.avatarImageView];
|
||||
[self.contentView addSubview:self.nameLabel];
|
||||
[self.contentView addSubview:self.contentLabel];
|
||||
[self.contentView addSubview:self.timeLabel];
|
||||
[self.contentView addSubview:self.pinIconView];
|
||||
|
||||
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.contentView).offset(16);
|
||||
make.centerY.equalTo(self.contentView);
|
||||
make.width.height.mas_equalTo(50);
|
||||
}];
|
||||
|
||||
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.avatarImageView.mas_right).offset(12);
|
||||
make.top.equalTo(self.avatarImageView).offset(2);
|
||||
make.right.lessThanOrEqualTo(self.timeLabel.mas_left).offset(-8);
|
||||
}];
|
||||
|
||||
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.nameLabel);
|
||||
make.top.equalTo(self.nameLabel.mas_bottom).offset(4);
|
||||
make.right.lessThanOrEqualTo(self.timeLabel.mas_left).offset(-8);
|
||||
}];
|
||||
|
||||
[self.pinIconView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.equalTo(self.contentView).offset(-16);
|
||||
make.top.equalTo(self.nameLabel);
|
||||
make.width.height.mas_equalTo(16);
|
||||
}];
|
||||
|
||||
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.equalTo(self.contentView).offset(-16);
|
||||
make.bottom.equalTo(self.contentLabel);
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)configWithAvatar:(NSString *)avatarUrl
|
||||
name:(NSString *)name
|
||||
content:(NSString *)content
|
||||
time:(NSString *)time
|
||||
isPinned:(BOOL)isPinned {
|
||||
if (avatarUrl.length > 0) {
|
||||
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:avatarUrl]
|
||||
placeholderImage:KBAvatarPlaceholderImage];
|
||||
} else {
|
||||
self.avatarImageView.image = KBAvatarPlaceholderImage;
|
||||
}
|
||||
self.nameLabel.text = name;
|
||||
self.contentLabel.text = content;
|
||||
self.timeLabel.text = time;
|
||||
self.pinIconView.hidden = !isPinned;
|
||||
|
||||
// 如果有置顶图标,时间标签需要调整位置
|
||||
if (isPinned) {
|
||||
[self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.equalTo(self.contentView).offset(-16);
|
||||
make.bottom.equalTo(self.contentLabel);
|
||||
}];
|
||||
} else {
|
||||
[self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.equalTo(self.contentView).offset(-16);
|
||||
make.bottom.equalTo(self.contentLabel);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Lazy Load
|
||||
|
||||
- (UIImageView *)avatarImageView {
|
||||
if (!_avatarImageView) {
|
||||
_avatarImageView = [[UIImageView alloc] init];
|
||||
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
|
||||
_avatarImageView.layer.cornerRadius = 25;
|
||||
_avatarImageView.layer.masksToBounds = YES;
|
||||
_avatarImageView.backgroundColor = [UIColor colorWithHex:0xF5F5F5];
|
||||
}
|
||||
return _avatarImageView;
|
||||
}
|
||||
|
||||
- (UILabel *)nameLabel {
|
||||
if (!_nameLabel) {
|
||||
_nameLabel = [[UILabel alloc] init];
|
||||
_nameLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
|
||||
_nameLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
|
||||
}
|
||||
return _nameLabel;
|
||||
}
|
||||
|
||||
- (UILabel *)contentLabel {
|
||||
if (!_contentLabel) {
|
||||
_contentLabel = [[UILabel alloc] init];
|
||||
_contentLabel.font = [UIFont systemFontOfSize:14];
|
||||
_contentLabel.textColor = [UIColor colorWithHex:0x9F9F9F];
|
||||
_contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
||||
}
|
||||
return _contentLabel;
|
||||
}
|
||||
|
||||
- (UILabel *)timeLabel {
|
||||
if (!_timeLabel) {
|
||||
_timeLabel = [[UILabel alloc] init];
|
||||
_timeLabel.font = [UIFont systemFontOfSize:12];
|
||||
_timeLabel.textColor = [UIColor colorWithHex:0x9F9F9F];
|
||||
_timeLabel.textAlignment = NSTextAlignmentRight;
|
||||
}
|
||||
return _timeLabel;
|
||||
}
|
||||
|
||||
- (UIImageView *)pinIconView {
|
||||
if (!_pinIconView) {
|
||||
_pinIconView = [[UIImageView alloc] init];
|
||||
_pinIconView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
// 使用系统图标或自定义图标
|
||||
if (@available(iOS 13.0, *)) {
|
||||
_pinIconView.image = [UIImage systemImageNamed:@"pin.fill"];
|
||||
_pinIconView.tintColor = [UIColor colorWithHex:0x9F9F9F];
|
||||
}
|
||||
_pinIconView.hidden = YES;
|
||||
}
|
||||
return _pinIconView;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user