Swift: ShinobiDataGrid Get Header Click/Selected


#1

I have a ShinobiDataGrid with many data. And I want to implement Sorting By each column. I don’t see any function like headerselected or something like header respon. I read some question in here regarding with my case but its always write with Obj-C so its makes unclear for me (refference). Any suggest or answer will help for me. Thanks in Advance


#2

Hi MrX,

The delegate method I think you’re looking for is shinobiDataGrid(_ grid: ShinobiDataGrid, didChangeSortOrderFor column: SDataGridColumn, from oldSortOrder: SDataGridColumnSortOrder). To enable sorting for a column, you need to set the column’s sortMode to SDataGridColumnSortModeBiState (or SDataGridColumnSortModeTriState if there’s a natural ordering to the data), then implement the didChangeSortOrder... method. Your implementation should update the order of your data, then call grid.reload().

Alternatively you could try using the SDataGridDataSourceHelper class which can handle sorting for you.

Kind regards,

Alison


#3

Hi aclarke, thanks for your answer.
But first I want to know, how can i show the arrow of the header column? I found the

column?.headerCell().showArrow(for: <SDataGridColumnSortOrder>, sortMode: <SDataGridColumnSortMode>)

But I dont know how to use that. Can you give me a simple example in swift. Thanks

I got the arrow with this :

column?.sortMode = SDataGridColumnSortModeBiState
column?.sortOrder = SDataGridColumnSortOrderAscending

But the arrow its collision with the text.


#4

Hi MrX,

To make use of the showArrow method you’ll need to subclass SDataGridHeaderCell and override that method. There’s an example of such a class at ToDoListHeaderCell.m in our play-todo-list sample on GitHub. I’ve used Swiftify to convert it to Swift here:

class ToDoListHeaderCell: SDataGridHeaderCell {

    var arrowView: UIImageView?

    override func showArrow(for sortOrder: SDataGridColumnSortOrder, sortMode: SDataGridColumnSortMode) {
        // Remove old arrow
        arrowView?.removeFromSuperview()
        if (sortMode == SDataGridColumnSortModeBiState) || (sortMode == SDataGridColumnSortModeTriState) {
            var arrow: UIImage?
            if sortOrder == SDataGridColumnSortOrderAscending {
                arrow = UIImage(named: "sort_up")
            }
            else if sortOrder == SDataGridColumnSortOrderDescending {
                arrow = UIImage(named: "sort_down")
            }
            else {
                arrow = UIImage(named: "no_sort")
            }
        
            // Create view from image and position at right middle of cell
            arrowView = UIImageView(image: arrow)
            arrowView?.center = CGPoint(x: CGFloat(frame.size.width - 20), y: CGFloat(frame.size.height / 2))
            addSubview(arrowView!)
        }
    }
}

Then you just need to set your column’s headerCellType to ToDoListHeaderCell.self and your grid will use this header cell.

I hope that helps.

Alison