Swift - How to use SDataGridDataSourceHelper


#1

Hi,

I am having trouble getting SDataGridDataSourceHelper to work in Swift

I add SDataGridDataSourceHelperDelegate to my controller class

class GradingController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, SDataGridDataSourceHelperDelegate {

and I created my grid and added SDataGridDataSourceHelper in my viewDidLoad:

let grid = ShinobiDataGrid(frame: self.view.bounds)
        self.view.addSubview(grid!)
        
        grid?.numberOfFrozenColumns = 1
        grid?.defaultCellStyleForHeaderRow.font = UIFont (name: "Helvetica Neue", size: 18)
        
        let lots = SDataGridColumn(title: "Lot")
        lots?.propertyKey = "lot"
        grid?.addColumn(lots)
        
        let columnA = SDataGridColumn(title: "Column A")
        columnA?.propertyKey = "columnA"
        grid?.addColumn(columnA)
        
        let columnB = SDataGridColumn(title: "Column B")
        columnB?.propertyKey = "columnB"
        grid?.addColumn(columnB)
        
        let columnC = SDataGridColumn(title: "Column C")
        columnC?.propertyKey = "columnC"
        grid?.addColumn(columnC)
        
        let cameraColumn = SDataGridColumn(title: "")
        cameraColumn?.propertyKey = "cameraColumn"
        grid?.addColumn(cameraColumn)
        
        let helper = SDataGridDataSourceHelper(dataGrid: grid!)
        helper?.delegate = self
        
        var array = [DataObject.init(fromString: "lot", columnA: "colum a", columnB: "colum b", columnC: "colum c", cameraColumn: "camera")] as Array

        array.append(DataObject.init(fromString: "lot", columnA: "colum a", columnB: "colum b", columnC: "colum c", cameraColumn: "camera"))

        helper?.data = array

Added this method:

func dataGridDataSourceHelper(_ helper: SDataGridDataSourceHelper!, populateCell cell: SDataGridCell!, withValue value: Any!, forProperty propertyKey: String!, sourceObject object: Any!) -> Bool {
        
        return false
        
    }

and here is my DataObject:

class DataObject : NSObject
{

    @objc var lot: String
    @objc var columnA: String
    @objc var columnB: String
    @objc var columnC: String
    @objc var cameraColumn: String

    init(fromString lot: String, columnA: String, columnB: String, columnC: String, cameraColumn: String) {
        self.lot = lot
        self.columnA = columnA
        self.columnB = columnB
        self.columnC = columnC
        self.cameraColumn = cameraColumn
        super.init()
    }
}

But I get this error: > [SDataGridDataSourceHelper respondsToSelector:]: message sent to deallocated instance

What am I doing wrong? Are there any examples for Swift for SDataGridDataSourceHelper?


#2

Hi jsuske,

It looks like the issue is that you’re not holding on to your SDataGridDataSourceHelper instance. This means that when viewDidLoad exits, the helper is thrown away, so when the grid asks for its data, it tries to call a method on the deallocated instance of the helper.

If you create an instance property to hold your helper, it should work as expected.

Kind regards,

Alison


#3

That worked! Thank you very much!