Can't load UICollectionView from UITableViewCell

can definition
can verb
can synonym
can noun meaning
meaning of can in hindi
can.'' acronym
can't meaning
can band

My UITableViewController has cells with build-in UICollectionView. I wound want to move UIViceController logic into the cell class - JournalSceneCell. To implement it in the controller I assigned to the cell's variable imagesFilename a new value

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = ... as? JournalSceneCell
    cell.imagesFilename = Array(journalRef!.entries[indexPath.row].imagesFilenames)
    return cell
}

The images could be large, and to make the UITableViewController load as fast as possible I tried to perform loading images in the didSet section and also use a background thread:

class JournalSceneCell: UITableViewCell, UICollectionViewDataSource {

    @IBOutlet weak var entryTitle: UILabel!
    @IBOutlet weak var locationText: UILabel!
    @IBOutlet weak var desctiptionText: UILabel!
    @IBOutlet weak var editButton: UIButton!
    @IBOutlet weak var shareButton: UIButton!
    @IBOutlet var collectionViewRef: UICollectionView!

    convenience init() {
        self.init()
        self.collectionViewRef.dataSource = self
   }

    struct ImageEntry {
        var image:UIImage
        var imageURL:URL
    }

    let collectionCellID = "JournalCollectionCellID"

    var selectedImages = [ImageEntry]()

    var imagesFilename = [String]() {
        didSet {
            DispatchQueue.global(qos: .background).async {
                let documentURL = try! FileManager.default.url(for: .documentDirectory,
                                                               in: .userDomainMask,
                                                               appropriateFor: nil,
                                                               create: true)
                self.selectedImages.removeAll()
                for imageName in self.imagesFilename {
                    let imageURL = documentURL.appendingPathComponent(imageName)
                    let imageData = try! Data(contentsOf: imageURL)
                    let img = UIImage(data: imageData)!
                    self.selectedImages.append(ImageEntry(image: img, imageURL: imageURL))
                }
                DispatchQueue.main.async {
                    self.collectionViewRef.reloadData()
                }
            }
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return selectedImages.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionViewRef.dequeueReusableCell(withReuseIdentifier: collectionCellID,
                                                        for: indexPath) as? JournalCollectionCell else {
            fatalError("Cannot connect a collection cell")
        }

        cell.imageView.image = selectedImages[indexPath.row].image
        return cell

    }

}

The problem: JournalSceneCell does not call collectionView func from UICollectionViewDataSource at all. Everything is connected via IB, reuse identified was added, but nothing happened

Reload collection view in tableView Cell,It will work

cell.collectionViewRef.reloadData()

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

You need to set

override func awakeFromNib() {
   super.awakeFromNib()
   self.collectionViewRef.dataSource = self
   self.collectionViewRef.delegate = self
}

class JournalSceneCell: UITableViewCell, UICollectionViewDataSource,UICollectionViewDelegate {

Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

The convenience init() does not being called. Finally I solve the problem by adding a func loadImages(fileNameArray:[String]) intoJournalSceneCell:

func loadImages(fileNameArray:[String]) {
    self.imageSetView.dataSource = self
    DispatchQueue.global(qos: .background).async {
        let documentURL = try! FileManager.default.url(for: .documentDirectory,
                                                       in: .userDomainMask,
                                                       appropriateFor: nil,
                                                       create: true)
        self.selectedImages.removeAll()
        for imageName in fileNameArray {
            let imageURL = documentURL.appendingPathComponent(imageName)
            let imageData = try! Data(contentsOf: imageURL)
            let img = UIImage(data: imageData)!
            self.selectedImages.append(ImageEntry(image: img, imageURL: imageURL))
        }

        DispatchQueue.main.async {
            self.imageSetView.reloadData()
        }

    }
}

Later I just called it from tableView(_:cellForRowAt:)

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = .. as? JournalSceneCell,
    cell.loadImages(fileNameArray:Array({...}))
    return cell
}

CAN, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

I was having a similar issue and it was because the UICollectionView wasn't even being drawn in the UITableViewCell, so I had to set a minimum height NSLayoutConstraint on the collection view.

You probably need the height of the UICollectionView to be dynamic though, in this case, but I'm not entirely sure your best route for that.

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

can, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men� Can(stylised as CAN) was a German experimental rockband formed in Colognein 1968 by the core quartet of Holger Czukay(bass, tape editing), Irmin Schmidt(keyboards), Michael Karoli(guitar), and Jaki Liebezeit(drums).

The Official CAN / Spoon Records Website, can 2. (kăn). n. 1. A usually cylindrical metal container. 2. Find the latest Canaan Inc. (CAN) stock quote, history, news and other vital information to help you with your stock trading and investing.

Can definition and meaning, (1) Hold on a minute, I gotta visit the can (2) They threw the poor homeless guy in the can for pissing in the park. (3) Sgt: Get off your cans and start policing this� A Controller Area Network (CAN bus) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other's applications without a host computer.

Comments
  • I am going to try
  • Actually I tried it. It didn’t help. Also I don’t need the delegate
  • Why? This is images from the local gallery
  • sure your documents file has images or you need main bundle like let img = UIImage(named: "name.png")!
  • I checked it again - It has images
  • I am going to try. It was connected and constructed via InterfaceBuilder, but maybe something went wrong.