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

时间:2019-01-12 15:20:34

标签: ios swift

我正在将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
    }
}

问题是我不认为以“ [messages””方式访问它是正确的方式。如何在单元设置内访问这些数据结构,以便使用户信息始终保持最新状态?我遇到类似“无法将类型'[CustomerData]'的值分配给类型'CustomerData?”之类的错误,那么访问单元内这些数据结构的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

  

问题在于,我不认为以“消息”的方式访问它是正确的方式。

这不是事实。将row的{​​{1}}或item属性作为数据源数组中元素的索引传递是正确的获取特定元素的方法。

但是,您使用的是IndexPath,因此即使功能相同,也应使用UICollectionView属性而不是item

UITableView

row

UICollectionView

let item = dataSourceArray[indexPath.row]

但是您永远不要将某些单元格作为设置方法的其他方法的参数。

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

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()