请稍侯

swift实现的一个简单的cha

20 August 2023

Swift实现的一个简单的ChatView

以下是基于您提供的要求对ChatView进行改进和封装的示例代码:

import UIKit

// 定义消息类型枚举
enum MessageType {
    case image
    case emoji
    case text
    case link
}

// 定义消息结构体,包含各个属性
struct Message {
    let type: MessageType // 消息类型
    let iconUrl: String? // 头像图片链接(可选)
    let name: String? // 发送者名称(可选)
    let imageUrl: String? // 图片消息链接(可选)
    let text: String? // 文本内容(可选)
    let clickLink: String? // 点击链接(可选)
    let time: Date    // 时间戳

    init(type: MessageType, iconUrl: String?, name: String?, imageUrl: String?, text: String?, clickLink: String?, time: Date) {
        self.type = type
        self.iconUrl = iconUrl
        self.name = name
        self.imageUrl = imageUrl
        self.text = text
        self.clickLink = clickLink
        self.time = time

    }
}


class ChatViewController: UIViewController, UITableViewDataSource {

    var messages: [Message] = []
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self

    }

    func appendMessage(_message: Message) {
        messages.append(_message)

        let indexPath = IndexPath(row: message.count - 1, section; 0)
        tableView.insertRows(at: [indexPath], with: .bottom)
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)


    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }

    func tableView(tableView: UITableView, numberofRowsInSection section: Int) -> Int {
        return message.count;
    }

    func tableView(_ tableview: UITableView, cellForRowAt indexpath: IndexPath) -> UITableViewCell {
        let message = message[indexpath.row]
        let identifier = generateCellIdentifier(for: message.type)
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! ChatTableViewCell
        // 设置单元格内容
        cell.configure(with: message)
        return cell

    }

    func generateCellIdentifier(for type: MessageType) -> String {
        switch type {
        case .image:
            return "ImageMessageCell"
        case .emoji:
            return "EmojiMessageCell"
        case .text:
            return "TextMessageCell"
        case .link:
            return "LinkMessageCell"

        }
    }

}


class ChatTableViewCell: UIViewTableCell {

    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!

    func configure(with message: Message) {
        if let iconUrl =message.iconUrl,
        let url =URL(string; iconUrl),
        let data = try? Data(contentsOf: url) {
            iconImageView.image = UIImage(data: data)

        }else {
            iconImageView.image = nil;
        }

        nameLabel.text = message.name ?? ""

        contentLabel.text =message.text ?? ""
    }


}


在上面的示例代码中,我们添加了一个新的数据结构Message来表示聊天消息,并使用枚举类型MessageType来标识不同类型的消息。根据您提供的要求,我们创建了相应的自定义单元格类并通过调用其configure方法对单元格进行配置。

请注意,在此示例中,默认为每个消息类型创建了一个唯一的单元格标识符(例如”ImageMessageCell”、”EmojiMessageCell”等)。 您可以根据需要进一步定制和扩展这些自定义单元格,并确保在注册时使用正确的标识符。