How to Programmatically Create an Excel Spreadsheet View iOS Swift

The easiest way to achieve multiple columns like Spreadsheet Tables in Swift. If you're looking without a third party library, that's great.



BASReport create a table like custom layout for a collection view using horizontal and vertical scrolling with sticky rows and columns written in Swift programming . 

Please visit the demo BASReport spreadsheet,The main part is the BASReport folder in the demo files.

Project Files


Storyboard

  • Go to main.storyboard and add a blank UIView 
  • Open the Identity Inspector and type 'BASCustomReport' the 'class' field 
  • Make sure you have 'Spreadsheet' selected in 'Module' field



Minimum working example:


import UIKit

class ViewController: UIViewController {
    
    //Outlets
    @IBOutlet weak var basReport: BASCustomReport!
    //Variables
    var sampleData = [SampleData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Weekly Time Table For Every Hour"
        initialSetup()
    }
    func initialSetup() {
        var HeaderDict = OrderedDictionary<String, Bool>()
        HeaderDict["PERIOD"] = false ;  HeaderDict["MONDAY"] = false ;HeaderDict["TUESDAY"] = false ;HeaderDict["WEDNSDAY"] = false ;HeaderDict["THURSDAY"] = false; HeaderDict["FRIDAY"] = false;HeaderDict["SATURDAY"] = false;HeaderDict["SUNDAY"] = false
        self.basReport.arrGHeader = HeaderDict
        setData()
    }
    func setData() {
        if let path = Bundle.main.path(forResource: "Example", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                let tempData  = try JSONDecoder().decode(RootClass.self, from: data )
                self.sampleData = tempData.data!
                
                self.basReport.arrGKeys = [("PERIOD", .left,nil, nil) , ("MONDAY",.center,90.0 , nil) , ("TUESDAY",.center,90.0 , nil) , ("WEDNSDAY",.center,100.0 , nil) , ("THURSDAY",.center,90.0 , nil) , ("FRIDAY",.center,90.0 , nil) , ("SATURDAY",.center,90.0 , nil),("SUNDAY",.center,90.0 , nil) ]
                
                if basReport.arrGHeader.count == basReport.arrGKeys.count{
                    self.basReport.arrReportSummaryDict = arryToDict(array: self.sampleData)
                }
            } catch {
                // handle error
            }
        }
        
    }
}


BASReport is available through CocoaPods. To fallow BASReport CocoaPods











Post a Comment

1 Comments

  1. Your tutorial solved my problem. I was stuck for a long time. thanks you..

    ReplyDelete