1

I'm using JSQMessage library with the JSQMessageCollectionViewCell. How can i access the value of cell outside of method cellForItem atIndexPath.

Here is the method

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

    if Array_Messages[indexPath.item].senderId == senderId {
        cell.textView?.textColor = UIColor.white
        cell.cellBottomLabel.textInsets = UIEdgeInsetsMake(0, 0, 0, 35)
        let second = ClassMessages[indexPath.row].Time.doubleValue
        let time = Date(timeIntervalSince1970: second)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"

        cell.cellBottomLabel.text = "\(String(describing: newMess.Status!)) \(dateFormatter.string(from: time))"  
    } 
    return cell
}

I try to do this but got the error

@IBAction func btTrash(_ sender: Any) {
    let indexPath = IndexPath.init(row: 0, section: 0)
    let cell = collectionView.cellForItem(at: indexPath) as! JSQMessagesCollectionViewCell
    print("aaa \(cell?.cellBottomLabel.text)")
}

Error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional

How can i fix this? Thank for your helping !

Trung Nguyen
  • 138
  • 3
  • 14
  • Ideally it should work. Can you check whether first cell `IndexPath.init(row: 0, section: 0)` is created in `cellForRowAt` before pressing `btTrash`. – Dinesh Balasubramanian May 19 '18 at 03:56
  • thanks for your comment, but i dont know what you means? Can you please make it clearly ? – Trung Nguyen May 19 '18 at 04:08
  • Can you put print inside `override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { ` and check whether cell is created for indexPath of row 0 and section 0. – Dinesh Balasubramanian May 19 '18 at 04:21
  • a i know, may be the problem is here. Thank you for your replying – Trung Nguyen May 19 '18 at 04:29
  • Should the first line in the method not be to dequeue and cast a reusable cell? – Chris May 19 '18 at 07:58
  • @Chris: yes i know the problem is. Thank for your comment – Trung Nguyen May 19 '18 at 08:02
  • I believe the problem occur due to not properly deque the cell. Can you provide me the full implementation of super class so that i will provide you an explaination in good implementation. – CrazyPro007 May 19 '18 at 10:14
  • Yes bro, thank for your helping. I'm now building the Chat App. The super class is very long code. If you happy, i will send it via your email. Thank a lot – Trung Nguyen May 19 '18 at 10:20

3 Answers3

0

i suggest you to add this checkBox in storyBoard and make it hidden by setting true to its isHidden property, and on your controller take one global Bool set as false. so when your bar button is cliked make this variable true and reload the table.

and in cellForRowAtIndexPath check It like this.

var shouldShowCheckBox = false

func barButtonTaped(){
shouldShowCheckBox = !(shouldShowCheckBox)
}

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      btnCheckBox.isHidden = shouldShowCheckBox        

    }
Abhay Singh
  • 255
  • 1
  • 8
0

Swift 4, Swift 5

You can use cellForItem to access your cell outside of the collectionView delegate. In this case, the MerchantCell located in row 4.

if let merchantCell = collectionView.cellForItem(at: IndexPath(row: 4, section: 0)) as? MerchantCell {
    merchantCell.refreshMerchant()
}
Pengguna
  • 4,636
  • 1
  • 27
  • 32
-1

You can access something like that also.

class RegistrationVc: UIViewController {

    var objCell = RegistrationCell()

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RegistrationCell", for: indexPath) as! RegistrationCell
        objCell = cell
        return cell
    }
    @IBAction func btnContinue(_ sender: UIButton) {

            objCell.txtUserName.errorMessage = "please enter user name"
     }
kalpesh
  • 1,285
  • 1
  • 17
  • 30