代码之家  ›  专栏  ›  技术社区  ›  Lukas Bimba

快速访问单元设置内的数据结构

  •  -1
  • Lukas Bimba  · 技术社区  · 6 年前

    我正在将一个firebase数据快照附加到一个nsobject,其中的项目是“客户”、“员工”和“业务”。设置如下:

    var customerData = [CustomerData]()
    var employeeData = [EmployeeData]()
    var businessData = [BusinessData]()
    
    func getCustomerData() {
        Database.database().reference().child("user_profiles").observe(.childAdded, with: { snapshot in
            self.customerData.append(CustomerData(snapshot: snapshot))
        })
    }
    
    func getEmployeeData() {
        Database.database().reference().child("employees").observe(.childAdded, with: { snapshot in
            self.employeeData.append(EmployeeData(snapshot: snapshot))
        })
    }
    
    func getBusinessData() {
        Database.database().reference().child("Businesses").observe(.childAdded, with: { snapshot in
            self.businessData.append(BusinessData(snapshot: snapshot))
        })
    }
    

    客户、员工和业务的数据结构如下

    import UIKit
    import Firebase
    
    class CustomerData: NSObject {
    
    var customerName: String?
    var customerPicture: String?
    var customerUID: String?
    
    init(snapshot: DataSnapshot) {
        if let dictionary = snapshot.value as? [String: AnyObject] {
            customerName = dictionary["name"] as? String
            customerUID = dictionary["uid"] as? String
            customerPicture = dictionary["profPicString"] as? String
        }
    }
    }
    

    我只想访问单元内的快照数据,以保持消息详细信息的最新,如配置文件图片和名称。下面是我的手机设置:

     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCell
    
        cell.chatLogController = self
    
        let customer = customerData
        let employee = employeeData
        let business = businessData
    
        let message = messages[indexPath.row]
    
        cell.message = message
        cell.customer = customer
        cell.employee = employee
        cell.business = business
    
        setupChatMessageCell(cell,message,customer,employee,business)
    
        if let text = message.text {
            cell.textView.text = text
            cell.bubbleWidthAnchor?.constant = estimateSizeOfText(text).width + 32
            cell.textView.isHidden = false
        } else if message.imageUrl != nil {
            cell.bubbleWidthAnchor?.constant = 200
            cell.textView.isHidden = true
        }
    
        cell.playButton.isHidden = message.videoUrl == nil
    
        return cell
    }
    
        private func setupChatMessageCell(_ cell: ChatMessageCell, _ message: GroupMessage, _ customer: CustomerData, _ employee: EmployeeData, _ business: BusinessData) {
    
    
        if message.fromId == customer.customerUID {
            //outgoing messages
            cell.bubbleView.backgroundColor = ChatMessageCell.blueColor
            cell.textView.textColor = .white
            cell.bubbleLeftAnchor?.isActive = false
            cell.bubbleRightAnchor?.isActive = true
            cell.profileImageView.isHidden = true
            cell.nameLabel.textColor = .gray
            cell.nameRightAnchor?.isActive = true
            cell.nameLeftAnchor?.isActive = false
            cell.nameLabel.text = message.name?.description
            //cell.nameLabel.text = message.customerName
        } else if message.fromId == employee.employeeUID {
            //incoming messagese
            let customerImage = employee.employeePicture
            cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
            cell.profileImageView.isHidden = false
            cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
            cell.textView.textColor = .black
            cell.bubbleLeftAnchor?.isActive = true
            cell.bubbleRightAnchor?.isActive = false
            cell.profileImageView.isHidden = false
            cell.nameRightAnchor?.isActive = false
            cell.nameLeftAnchor?.isActive = true
            cell.nameLabel.textColor = .black
            cell.nameLabel.text = message.name?.description
        } else if message.fromId == business.businessUID {
            let customerImage = business.businessPicture
            cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
            cell.profileImageView.isHidden = false
            cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
            cell.textView.textColor = .black
            cell.bubbleLeftAnchor?.isActive = true
            cell.bubbleRightAnchor?.isActive = false
            cell.profileImageView.isHidden = false
            cell.nameRightAnchor?.isActive = false
            cell.nameLeftAnchor?.isActive = true
            cell.nameLabel.textColor = .black
            cell.nameLabel.text = message.name?.description
        }
    
        if let imageUrl = message.imageUrl {
            cell.messageImageView.loadImageUsingCacheWithUrlString(imageUrl)
            cell.messageImageView.isHidden = false
            cell.bubbleView.backgroundColor = .clear
        } else {
            cell.messageImageView.isHidden = true
        }
    }
    

    问题是,我不认为以“[index.path]”访问它是正确的方式,这正是我处理“消息”的方式。如何在单元设置中访问这些数据结构,以便始终保持用户信息的最新?“无法将类型”[CustomerData]“的值赋给类型”CustomerData?“之类的错误。”那么,访问单元内这些数据结构的正确方法是什么呢?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Robert Dresler Gustavo Vollbrecht    6 年前

    问题是,我不认为以“[index.path]”访问它是正确的方式,这正是我处理“消息”的方式。

    这不是真的。经过 row item 性质 IndexPath 由于数据源数组中元素的索引是获取特定元素的正确方法。

    但是,你在使用 UICollectionView ,所以您应该使用 项目 属性而不是 即使功能相同

    UITababVIEW

    let item = dataSourceArray[indexPath.row]
    

    ui集合视图

    let item = dataSourceArray[indexPath.item]
    

    但是,对于正在设置某个单元格的其他方法,不应将该单元格作为参数传递。

    相反,在集合视图的单元格子类中,创建用于设置单元格视图等的方法。

    class ChatMessageCell: UICollectionViewCell {
        ...
        var message: Message!
        ...
        func setupCell() {
            ... // here you can work with cell's properites e.g. message, ...
        }
    }
    

    …然后打电话进来 cellForItemAt

    在这个方法中,您应该更改与内容相关的内容。”“修饰”的内容,如更改视图颜色等。您可以将其设置在重写的内部 UICollectionViewCell 方法 prepareForReuse()

    override func prepareForReuse() {
        super.prepareForReuse()
        ...
    }