Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速访问单元设置内的数据结构_Ios_Swift - Fatal编程技术网

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

Ios 快速访问单元设置内的数据结构,ios,swift,Ios,Swift,我正在将Firebase数据快照附加到一个NSObject中,其中的项目包括“客户”、“员工”和“业务”。设置如下: var customerData = [CustomerData]() var employeeData = [EmployeeData]() var businessData = [BusinessData]() func getCustomerData() { Database.database().reference().child("user_profiles"

我正在将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
    }
}
}
let item = dataSourceArray[indexPath.row]
let item = dataSourceArray[indexPath.item]
我只想访问单元格内的快照数据,以保持我的消息详细信息最新,如配置文件图片和名称。以下是我的手机设置:

 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?”,那么访问单元格内这些数据结构的正确方法是什么

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

事实并非如此。将
indepath
row
item
属性作为数据源数组中元素的索引传递是获取特定元素的正确方法

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

UITableView

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
    }
}
}
let item = dataSourceArray[indexPath.row]
let item = dataSourceArray[indexPath.item]
UICollectionView

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
    }
}
}
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
的方法
prepareforeuse()中设置