2 Replies
      Latest reply on Feb 14, 2020 2:27 PM by Claude31
      donnist78 Level 1 Level 1 (0 points)

        Im trying to make a generated array of (6) numbers display per cell, of a collection view with reusable cells.

         

        The array is being generated with this:

         

        let original = [1,  2,  3,  4,  5,  6] 
        var currentArray = original 
        print("currentArray =", currentArray)  
        var stop = false 
        repeat { 
         currentArray = currentArray.map() { ($0 + 2) % 25 + 1}  
         print("currentArray =", currentArray)   
        stop = currentArray[0] == 25     
        } while !stop

         

         

        my Collection View is being setup here...

         

        <ViewController.swift>

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! discoCueCell
                cell.backgroundColor=UIColor.clear
        

         

         

        Any ideas???

        • Re: Is it possible to have an array display in cells of a collection view?
          Claude31 Level 8 Level 8 (8,145 points)

          The simplest would be to save the generated arrays in an array (of arrays):

           

          var arraysForDataSource : [[Int]] = []
          
          let original = [1,  2,  3,  4,  5,  6]
          var currentArray = original
          
          var stop = false
          repeat {
               currentArray = currentArray.map() { ($0 + 2) % 25 + 1}
               arraysForDataSource.append(currentArray)
               stop = currentArray[0] == 25   
          } while !stop

           

          And use it in delegate function

           

          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                  let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! discoCueCell
                  cell.backgroundColor=UIColor.clear
                  let arrayForCell = arraysForDataSource[indexPath.row]
               // Do whatever you need with the array…
          }
            • Re: Is it possible to have an array display in cells of a collection view?
              Claude31 Level 8 Level 8 (8,145 points)

              Does it answer your question ?

               

              How do you want to display the array ?

               

              If you just want the list of elements:

              - create a label

              - put the text in it.

               

              Could look like this:

               

              func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! discoCueCell
                      cell.backgroundColor=UIColor.clear
                      let arrayForCell = arraysForDataSource[indexPath.row]
              
                   // 1. Remove existing subviews, to avoid adding another one each time
                      for view in cell.contentView.subviews {
                          view.removeFromSuperview()
                      }
              
                   //   2. Create the label, and position and size as needed
                      let label = UITextView(frame: CGRect(x: 5, y: 0, width: 60, height: 20))     // adapt as needed
                      label.isUserInteractionEnabled = false  // Don't allow editing of a cell
              
                   // 3. Write the array in the label
                      label.text = arrayForCell.reduce("") { text, val in text == "" ? "\(val)" : "\(text),\(val)" }
                   
                   // 4. Add the label to the contentView
                      cell.contentView.addSubview(label)
              
                   // 5. return cell     
                   return cell
              }