In order to have a good interactive experience, I believe that everyone in the treatment of ScrollView no data when the prompt page will use some third-party to customize, the most typical is the use of dznemptydataset. But it is not so good to write a bunch of code related to Dznemptydatasetdelegate,dznemptydatasetsource in every interface, and that's usually the way to avoid it. And Swift, in addition to object-oriented programming, can also be oriented toward protocol programming. Can you also use the agreement to solve the situation? Hey, this can be, then we'll try to do it through the protocol to avoid this situation, and implement a line of code to add a blank page function Preface
If you have questions about the protocol, you can look at my two previous articles.
Ios-swift protocol-oriented programming (I.)
Ios-swift protocol-oriented Programming (II)
Before the article mentioned, the agreement in addition to the normative role, there is another use, is to empower. Our goal now is to have the target controller or the target view follow our protocol and have the ability to implement a blank page. first, the basic realization 1. Create Agreement
mark:-empty View Placeholder protocol public
protocol lxfemptydatasetable {
}
2, to determine the class-oriented
To determine which class we are facing, general TableView or CollectionView are written in the controller, then the class we are facing is defined as Uiviewcontroller, and maybe someone wrote it in UIView, But let's just press Uiviewcontroller to write here.
mark:-Uiviewcontroller-Empty View placeholder protocol public
extension lxfemptydatasetable where Self:uiviewcontroller {
//3, implementation The method is written here
}
3, the definition of functional methods
Pass the ScrollView in, let's define a way to do something secretly
Func Lxf_emptydataset (_ Scrollview:uiscrollview) {
scrollview.emptydatasetdelegate = self
Scrollview.emptydatasetsource = Self
}
4. Set up data source and proxy
In 3, define the functional method to set the delegate and source to self, and the Protocol is unable to comply with the other protocol, so what to comply with the corresponding agreement. To understand that self here refers to Uiviewcontroller, in view of the possibility of uiview, here I let the father of all objects nsobject to abide by, and implement the corresponding data source methods and proxy methods
Extension Nsobject:dznemptydatasetdelegate, Dznemptydatasetsource {public
func image (Foremptydataset ScrollView : uiscrollview!) -> uiimage! {
///return hint picture
} public
func title (Foremptydataset scrollview:uiscrollview!)-> nsattributedstring! {
//Set Rich text title
} public
func verticaloffset (foremptydataset scrollview:uiscrollview!)-> cgfloat {
/ /Set longitudinal offset
}
}
Two, the custom blank page
After the above steps, as long as let Uiviewcontroller comply with our agreement, and then call the Lxf_emptydataset method can be implemented data blank page. However, the way to write directly to death is very bad, sometimes some scenes are needed to make custom, then how to achieve customization. The protocol cannot have its own variables to store our customizations.
To make a qualification first, we need to use the overloaded method to complete the function, to achieve high customization, but also to use the default customization.
Back to the topic, using Userdefaults to achieve it. Yes, but it's troublesome, because Userdefaults is a single example, the whole process sharing this resource, if your current controller comply with our agreement lxfemptydatasetable and make a custom, So what do you do when the next controller uses the default customization after complying with the protocol? Also want to distinguish ScrollView, that must save the current ScrollView, after exiting the current controller also want to put the corresponding things empty. Okay, well, then what's the best thing for you to do?
Solution: Expand Uiscrollview ... There was no discovery. , very coincidentally, we define the method Lxf_emptydataset need outside will Uiscrollview pass in, in Dznemptydataset data source method and proxy method also have ScrollView. It would be nice to have uiscrollview to carry our custom. 1. Define custom-related enumerations
Here I define the common custom-related enumerations
public enum Lxfemptydatasetattributekeytype {
///longitudinal offset ( -50) cgfloat case
verticaloffset
///prompt (no data String case
tipstr
///the Font (SYSTEM15) uifont case
tipfont
///prompt Color (D2D2D2) Uicolor case
tipcolor
///hint diagram (lxfemptydatapic) uiimage case
tipimage
///allow scrolling (true) Bool
Case Allowscroll
}
2. Expand Uiscrollview
Define a custom-related property dictionary for Uiscrollview
Extension Uiscrollview {
private struct Associatedkeys {
static var lxf_emptyattributedict:[ Lxfemptydatasetattributekeytype:any]?
}
Attribute Dictionary
var lxf_emptyattributedict: [Lxfemptydatasetattributekeytype:any]? {Get
{return
objc_getassociatedobject (self, &associatedkeys.lxf_emptyattributedict) as? [Lxfemptydatasetattributekeytype:any]
}
set {
objc_setassociatedobject (self, &associatedkeys.lxf_emptyattributedict, newvalue as [ Lxfemptydatasetattributekeytype:any]?, objc_associationpolicy.objc_association_retain_nonatomic)
}
}
}
3. Perfect Lxf_emptydataset method
Here we have the outside world to customize our own blank pages by means of closures.
mark:-Uiviewcontroller-Empty View placeholder protocol public
extension lxfemptydatasetable where Self:uiviewcontroller {
func lxf _emptydataset (_ Scrollview:uiscrollview, Attributeblock: (()-> ([Lxfemptydatasetattributekeytype:any]))? = nil) { C2/>scrollview.lxf_emptyattributedict = Attributeblock!= nil? attributeblock! (): nil
scrollview.emptydatasetdelegate = self
scrollview.emptydatasetsource = self
}
}
4. Use custom attribute dictionary
Here is an example of how to return the hint picture.
public func image (Foremptydataset scrollview:uiscrollview!)-> uiimage! {
guard Let tipimg = scrollview.lxf_emptyattributedict? [. tipimage] as? UIImage else {return
uiimage (named: ' Lxfemptydatapic ')
} return
tipimg
}
5, the outside use posture
Class Lxfemptydemocontroller:uiviewcontroller {
override func Viewdidload () {
super.viewdidload ()
Initui ()
}
}
extension lxfemptydemocontroller:lxfemptydatasetable {
fileprivate func Initui () { Let
TableView = UITableView ()
//...
High-Custom
Self.lxf_emptydataset (TableView) {()-> ([Lxfemptydatasetattributekeytype:any]) in return
[
tipstr: "Yo yo Yo",
. verticaloffset:-150,
. Allowscroll:false
]
}
//Default custom
// Self.lxf_emptydataset (TableView)
}
}
third, open Source Library
I'm finishing this process once and making it into a library called Lxfprotocoltool and uploading it to GitHub. You can use the Cocoapods method to install
Pod ' Lxfprotocoltool '
I have also integrated the features mentioned in Ios-swift protocol-oriented Programming (ii) with the convenience of loading xib by protocol. Everyone can according to their own needs in the Podfile to specify the features to install Xib load
Pod ' lxfprotocoltool/lxfnibloadable '
Blank View
Pod ' lxfprotocoltool/lxfemptydatasetable '
The purpose of creating this library is to implement some practical functions easily and quickly by protocol, but it will gradually increase in the future, maybe you have some functions you want to implement, you can also put forward, like to give a star to encourage me