1- # 实现新类型自定义消息 Cell
1+ # 实现新类型的自定义消息 Cell
22
33本文以红包消息为例介绍如何添加一种新类型的自定义消息 Cell。
44
5- 1 . 根据需求继承 ` EaseChatUIKit ` 中的自定义消息 Cell。
5+ ## 步骤一 继承自定义消息 Cell
6+
7+ 根据需求继承 ` EaseChatUIKit ` 中的自定义消息 Cell。
68
79``` Swift
810import UIKit
@@ -31,7 +33,9 @@ class RedPackageCell: CustomMessageCell {
3133
3234```
3335
34- 2 . 根据需求继承 ` EaseChatUIKit ` 中的 Cell 的渲染模型 ` MessageEntity ` ,并指定气泡大小,其中 ` redPackageIdentifier ` 为红包的自定义消息的 event事件。
36+ ## 步骤二 继承 Cell 的渲染模型
37+
38+ 根据需求继承 ` EaseChatUIKit ` 中的 Cell 的渲染模型 ` MessageEntity ` ,并指定气泡大小,其中 ` redPackageIdentifier ` 为红包的自定义消息的 ` event ` 事件。
3539
3640``` Swift
3741import UIKit
@@ -64,17 +68,19 @@ final class MineMessageEntity: MessageEntity {
6468}
6569```
6670
67- 3 . 添加发送附件消息的类型。
71+ ## 步骤三 添加附件消息类型
6872
69- 例如,增加发送红包消息 。
73+ 添加附件消息的类型, 例如,增加红包消息 。
7074
7175``` Swift
7276
7377 let redPackage = ActionSheetItem (title : " 红包" .chat .localize , type : .normal ,tag : " Red" ,image : UIImage (named : " photo" , in : .chatBundle , with : nil ))
7478 Appearance.chat .inputExtendActions .append (redPackage)
7579```
7680
77- 4 . 在继承的 ` MessageListController ` 处理新增的附件消息类型的点击。
81+ ## 步骤四 处理新增的附件消息类型的点击事件
82+
83+ 继承 ` MessageListController ` ,处理新增的附件消息类型的点击事件。
7884
7985``` Swift
8086class CustomMessageListController : MessageListController {
@@ -101,9 +107,11 @@ let redPackageIdentifier = "redPackage"
101107
102108```
103109
104- ![ img] ( /images/uikit/chatuikit/ios/configurationitem/chat/red_package_attachment.png )
110+ ![ img] (/images/uikit/chatuikit/ios/configurationitem/chat/red_package_attachment.png =350x750 )
105111
106- 5 . 给 ` EaseChatUIKit ` 中的 ` MessageListViewModel ` 增加发送红包消息的方法。
112+ ## 步骤五 增加发送新类型附件消息的方法
113+
114+ 在 ` EaseChatUIKit ` 的 ` MessageListViewModel ` 中增加发送红包消息的方法。
107115
108116``` Swift
109117extension MessageListViewModel {
@@ -135,9 +143,11 @@ extension MessageListViewModel {
135143
136144```
137145
138- ![ img] ( /images/uikit/chatuikit/ios/configurationitem/chat/red_package_send.png )
146+ ![ img] (/images/uikit/chatuikit/ios/configurationitem/chat/red_package_send.png =350x750)
147+
148+ ## 步骤六 注册继承的对象
139149
140- 6 . 将上述继承的对象初始化后,在 ` EaseChatUIKit ` 中进行注册。
150+ 将上述继承的对象初始化后,在 ` EaseChatUIKit ` 中进行注册。
141151
142152``` Swift
143153
@@ -147,7 +157,10 @@ extension MessageListViewModel {
147157 ComponentsRegister.shared .registerCustomizeCellClass (cellType : RedPackageCell.self )
148158```
149159
150- - 这里 ` ComponentsRegister.shared.Conversation = MineConversationInfo.self ` 是为了修改自定义消息在会话列表中。会话收到新消息时显示的内容这里暂定为显示 "[ 红包] ",示例代码如下,主要更改在非文本消息类型的 ` else ` 中根据自定义消息的 ` event ` 显示对应的内容。
160+ 这里的 ` ComponentsRegister.shared.Conversation = MineConversationInfo.self ` 用于在会话列表中展示收到的新类型的自定义消息内容。
161+
162+ 例如,在以下示例代码中,会话中收到新消息时显示为 "[ 红包] ",主要调整了在非文本消息类型的 ` else ` 中根据自定义消息的 ` event ` 显示对应的内容。
163+
151164``` Swift
152165import UIKit
153166import EaseChatUIKit
@@ -222,4 +235,4 @@ final class MineConversationInfo: ConversationInfo {
222235
223236```
224237
225- ![ img] ( /images/uikit/chatuikit/ios/configurationitem/chat/red_package_receive.png )
238+ ![ img] (/images/uikit/chatuikit/ios/configurationitem/chat/red_package_receive.png =350x750 )
0 commit comments