How I can update data inside messageCollectionView cell swift?

219 views Asked by At

I would like to show loaded image in the cell. I tried to make it in such way:

messages.filter { (mType) -> Bool in
                        mType.messageId == "\(message.id)"
                    }.first?.kind = .photo(ImageMediaItem(image: UIImage(systemName: "photo")!.withTintColor(UIColor.gray, renderingMode: .automatic)))

where:

var messages = [MessageType]()

but I saw a message which said:

Cannot assign to property: 'kind' is a get-only property

So I decided to add setter/getter to this property:

struct Message: MessageType {
    var sender: SenderType
    var messageId: String = ""
    var sentDate: Date
    var kind: MessageKind{
        get {
            return self.kind
        }
        set (newValue) {
            self.kind = newValue
        }
    }
}

I used such scope for adding data to chat list:

messages.append(Message(sender: message.fromId == selectedContactID ? otherSender! : currentUser!, messageId: "\(message.id ?? -1)", sentDate: Date().addingTimeInterval(-8600000), kind: mKind))

but after changes mentioned above I saw such message:

Extra argument 'kind' in call

I tried to look through documentation but I didn't manage to find smth useful for my situation. Maybe someone knows how to solve this problem?

1

There are 1 answers

6
Shehata Gamal On BEST ANSWER

You need to get index of the object you need to change with a struct

if let index = messages.firstIndex(where:{ $0.messageId == "\(message.id)" }) {
   messages[index].kind = .photo(ImageMediaItem(image: UIImage(systemName: "photo")!.withTintColor(UIColor.gray, renderingMode: .automatic)))
}

and for Extra argument 'kind' in call a computed property shouldn't be in init in addition to that in your case you don't need to make it computed property as you implement what's already exist by default