DZNEmptyDataSet 框架阅读
2021-02-24 本文已影响0人
CodingTom
DZNEmptyDataSet
1.代码使用简单:主要逻辑在UIScrollView+EmptyDataSet分类中完成。使用时只需要设置控制器为其数据源和代理,并实现相应的代理方法即可。
2.对runtime合理使用:利用runtime的关联功能实现分类中属性的getter、setter;利用runtime的method 的 IMP指针交互功能 进行reloadData等方法的扩展, 注入了额外的逻辑代码。
3.修改对空白列表占位视图的响应链传递路径。
4.采用NSLayoutConstraint+VFL(Visual Format Language)“可视化格式语言”进行设置约束
// Based on Bryce Buchanan's swizzling technique http://blog.newrelic.com/2014/04/16/right-way-to-swizzle/
// And Juzzin's ideas https://github.com/juzzin/JUSEmptyViewController
void dzn_original_implementation(id self, SEL _cmd)
{
// Fetch original implementation from lookup table
Class baseClass = dzn_baseClassToSwizzleForTarget(self);
NSString *key = dzn_implementationKey(baseClass, _cmd);
NSDictionary *swizzleInfo = [_impLookupTable objectForKey:key];
NSValue *impValue = [swizzleInfo valueForKey:DZNSwizzleInfoPointerKey];
IMP impPointer = [impValue pointerValue];
// We then inject the additional implementation for reloading the empty dataset
// Doing it before calling the original implementation does update the 'isEmptyDataSetVisible' flag on time.
[self dzn_reloadEmptyDataSet];
// If found, call original implementation
if (impPointer) {
((void(*)(id,SEL))impPointer)(self,_cmd);
}
}
NSString *dzn_implementationKey(Class class, SEL selector)
{
if (!class || !selector) {
return nil;
}
NSString *className = NSStringFromClass([class class]);
NSString *selectorName = NSStringFromSelector(selector);
return [NSString stringWithFormat:@"%@_%@",className,selectorName];
}
Class dzn_baseClassToSwizzleForTarget(id target)
{
if ([target isKindOfClass:[UITableView class]]) {
return [UITableView class];
}
else if ([target isKindOfClass:[UICollectionView class]]) {
return [UICollectionView class];
}
else if ([target isKindOfClass:[UIScrollView class]]) {
return [UIScrollView class];
}
return nil;
}
- (void)swizzleIfPossible:(SEL)selector
{
// Check if the target responds to selector
if (![self respondsToSelector:selector]) {
return;
}
// Create the lookup table
if (!_impLookupTable) {
_impLookupTable = [[NSMutableDictionary alloc] initWithCapacity:3]; // 3 represent the supported base classes
}
// We make sure that setImplementation is called once per class kind, UITableView or UICollectionView.
for (NSDictionary *info in [_impLookupTable allValues]) {
Class class = [info objectForKey:DZNSwizzleInfoOwnerKey];
NSString *selectorName = [info objectForKey:DZNSwizzleInfoSelectorKey];
if ([selectorName isEqualToString:NSStringFromSelector(selector)]) {
if ([self isKindOfClass:class]) {
return;
}
}
}
Class baseClass = dzn_baseClassToSwizzleForTarget(self);
NSString *key = dzn_implementationKey(baseClass, selector);
NSValue *impValue = [[_impLookupTable objectForKey:key] valueForKey:DZNSwizzleInfoPointerKey];
// If the implementation for this class already exist, skip!!
if (impValue || !key || !baseClass) {
return;
}
// Swizzle by injecting additional implementation
Method method = class_getInstanceMethod(baseClass, selector);
IMP dzn_newImplementation = method_setImplementation(method, (IMP)dzn_original_implementation);
// Store the new implementation in the lookup table
NSDictionary *swizzledInfo = @{DZNSwizzleInfoOwnerKey: baseClass,
DZNSwizzleInfoSelectorKey: NSStringFromSelector(selector),
DZNSwizzleInfoPointerKey: [NSValue valueWithPointer:dzn_newImplementation]};
[_impLookupTable setObject:swizzledInfo forKey:key];
}