Displaying a table (in the sense of an html / Excel table) in an iOS application in Swift

I was looking for solutions on how to do this, but I got a little confused.

I would like to display data tables in my application, and not in the sense of Apple TableView, more like tables that you find in Excel or in HTML. Moreover, in my application I will have several tables , like this on different pages, that I want to style in the same way , not all with the same number of rows or columns . These tables may appear on the screen along with images or texts, possibly never by themselves. (so I need an element that I can add to the UIViewController)

Basically a table like this (I put it aside from Google images because my data is confidential) enter image description here

I have a theory on how to do this, but I'm not sure how to do it correctly or exactly.

My theory is that I have to subclass UICollectionView, let me call MyTablesView. MyTablesView will have a variable called data. My pages / VC containing the table will have an instance of MyTablesView and pass it to this variable. MyTablesView takes care of styling and displaying this data.

Swift, , , . , UICollectionView. : http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1, UICollectionViewController, UICollectionView, , .

, , ...   class MyTablesView: UICollectionView {     var data = [String]   } ( , 100% , , , , - ...)

, , : UICollectionView , ViewController?

, - . .

+4
1

, , SO, , : Swift

, Stack Overflow, ?


, , , , , , , . , . , , , , numberOfSectionsInCollectionView . , .

  • VC, UICollectionView. UILabel.

  • VC UICollectionViewDataSource UICollectionViewDelegate data, . ctrl + UICollectionView mytable.

    class MyView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
      @IBOutlet weak var mytable: UICollectionView! 
      var data = [[String]]()
    
      override func viewDidLoad() {
        data = [["first", "1", "2"], ["second", "3", "4"]]
      }
    
    // --- UICollectionViewDataSource protocol ---
    
      //returns number of sections = subarrays in data
      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
        return data.count
      }
    
      //returns number of items in a section= number of elements in a subarray in data    
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data[section].count
      }
    
    
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyTableCells
    
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.myLabel.text = data[indexPath.section][indexPath.item]
    
        return cell
      }
    
    // --- UICollectionViewDelegate protocol ---
      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
      }
    

    }

  • UICollectionViewCell. ctrl + UILabel UICollectionView myLabel.

    import Foundation
    class MyTableCells: UICollectionViewCell {  
      @IBOutlet weak var myLabel: UILabel! 
    }
    

VC ( UICollectionView, VC)

VC , UIViewController, VC , .

+6

Source: https://habr.com/ru/post/1606388/


All Articles