EmptyDataSet-Swift icon indicating copy to clipboard operation
EmptyDataSet-Swift copied to clipboard

EmptyDataSetSource function not get called on subclass if parent class has not implement that EmptyDataSetSource function

Open Oowomn opened this issue 5 years ago • 1 comments

For example, a viewcontroller called ParentVC has implement EmptyDataSetSource with below delegate function.

func title(forEmptyDataSet _: UIScrollView) -> NSAttributedString? {
	return NSAttributedString(string: "title")
}

Now, a viewcontroller called SubVC which inherited ParentVC. if I want to show a button when tableview is empty. I added this delegate method.

func buttonTitle(forEmptyDataSet _: UIScrollView, for _: UIControl.State) -> NSAttributedString? {
	return NSAttributedString(string: "Add")
}

The result is the subclass delegate method will not be called. There is no button shown. This is not I expected.

And I test that if I add the function above at ParentVC and override in SubVC. Then it works.

In ParentVC
func buttonTitle(forEmptyDataSet _: UIScrollView, for _: UIControl.State) -> NSAttributedString? {
	return nil
}
In SubVC
override func buttonTitle(forEmptyDataSet _: UIScrollView, for _: UIControl.State) -> NSAttributedString? {
	return NSAttributedString(string: "Add")
}

It is strange that I need to implement the function into ParentVC which is useless to ParentVC. If I have many different subclass of ParentVC which has their own special handling. I have to add all those method on ParentVC first and override at subclass then.

Oowomn avatar May 23 '19 07:05 Oowomn

I think it's because I extend the EmptyDataSetSource to provide default implementations. Just like this question.

public extension EmptyDataSetSource {
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
        return nil
    }
}

Xiaoye220 avatar May 24 '19 08:05 Xiaoye220