xmartlabs/Eureka

onCellUnHighlight doesn't get called unless onCellHighlight is also defined

jefflai opened this issue · 4 comments

The onCellUnHighlight function for a Row doesn't get called if the onCellHighlight function is not also specified for the Row. This is confusing because the documentation doesn't state that both of these functions have to be defined for onCellUnHighlight to work.

This is the debug code I used to find this bug.

form +++ Section("Debug")
            <<< TextRow("debugTextRow1") {
                    $0.title = "Debug Text Row 1"
                }.onCellHighlight { cell, row in
                    print("text onCellHighlight 1")
                }.onCellUnHighlight{ cell, row in
                    print("text onCellUnHighlight 1")
                }
            <<< TextRow("debugTextRow2") {
                    $0.title = "Debug Text Row 2"
                }.onCellUnHighlight{ cell, row in
                    print("text onCellUnHighlight 2")
                }

Tapping on debugTextRow1 and then tapping outside debugTextRow1 to trigger onCellUnHighlight works as expected with both onCellHighlight and onCellUnHighlight defined.

However, doing the same steps with debugTextRow2 doesn't work as expected. The onCellUnHighlight function for debugTextRow2 is never called.

I'm using XCode 7.1.1 and the project has a deployment target of iOS 9.1

@jefflai Actually this is not an issue. This is how onCellHighlight and onCellUnHighlight work by default.

Your custom onCellUnHighlight is not being invoked because each time the cell is highlighted we override onCellUnHighlight as shown below.

public final class TextRow: _TextRow, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
        onCellHighlight { cell, row  in
            let color = cell.textLabel?.textColor
            row.onCellUnHighlight { cell, _ in
                cell.textLabel?.textColor = color
            }
            cell.textLabel?.textColor = cell.tintColor
        }
    }
}

The reason we override by default the onCellUnHighlight from inside onCellHighlight is to put back the original label text color since it can be changed at any time.

So in order to avoid this you must set up a onCellHighlight function in addition to onCellUnHighlight. Your custom onCellHighlight will not override onCellUnHighlight.

Regards

@mtnbarreto ok that makes sense. perhaps the ReadMe should be updated to explain this