#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
self.tableView.emptyDataSetSource = self;
OBJC_EXPORT id _Nullable objc_msgSend(id _Nullable self, SEL _Nonnull op, ...) OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
平時用的runtime函數交換方法會改變原始函數的方法名,其對應的C函數就是參數SEL。app
void method_exchangeImplementations(方法m1,方法m2)
- (void)setEmptyDataSetSource:(id<DZNEmptyDataSetSource>)datasource { if (!datasource || ![self dzn_canDisplay]) { [self dzn_invalidate]; } objc_setAssociatedObject(self, kEmptyDataSetSource, [[DZNWeakObjectContainer alloc] initWithWeakObject:datasource], OBJC_ASSOCIATION_RETAIN_NONATOMIC); // We add method sizzling for injecting -dzn_reloadData implementation to the native -reloadData implementation [self swizzleIfPossible:@selector(reloadData)]; // Exclusively for UITableView, we also inject -dzn_reloadData to -endUpdates if ([self isKindOfClass:[UITableView class]]) { [self swizzleIfPossible:@selector(endUpdates)]; } }
static NSMutableDictionary *_impLookupTable; static NSString *const DZNSwizzleInfoPointerKey = @"pointer"; static NSString *const DZNSwizzleInfoOwnerKey = @"owner"; static NSString *const DZNSwizzleInfoSelectorKey = @"selector"; - (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; } } } //1.根據target 返回對應的類class Class baseClass = dzn_baseClassToSwizzleForTarget(self); //2.根據class名和selector,建立一個dzn_implement組合key NSString *key = dzn_implementationKey(baseClass, selector); //3.根據class名和selector組合key,拿到交換的implement指針。 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); //4.將C函數dzn_original_implementation設置成Selector的新的IMP,並返回舊的IMP指針。 IMP dzn_newImplementation = method_setImplementation(method, (IMP)dzn_original_implementation); // Store the new implementation in the lookup table(源碼註解錯誤,應該是old implementation,能夠點擊函數method_setImplementation查看驗證) // 存儲舊的reload涵數指針IMP到全局查詢表_impLookupTable (正確註釋) NSDictionary *swizzledInfo = @{DZNSwizzleInfoOwnerKey: baseClass, DZNSwizzleInfoSelectorKey: NSStringFromSelector(selector), DZNSwizzleInfoPointerKey: [NSValue valueWithPointer:dzn_newImplementation]}; [_impLookupTable setObject:swizzledInfo forKey:key]; }
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); } }
- (void)dzn_reloadEmptyDataSet //空白視圖添加方法 if (!view.superview) { // Send the view all the way to the back, in case a header and/or footer is present, as well as for sectionHeaders or any other content if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) && self.subviews.count > 1) { [self insertSubview:view atIndex:0]; } else { [self addSubview:view]; } } //更新內部子視圖約束 [view setupConstraints];
- (void)setupConstraints { // First, configure the content view constaints // The content view must alway be centered to its superview NSLayoutConstraint *centerXConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterX]; NSLayoutConstraint *centerYConstraint = [self equallyRelatedConstraintWithView:self.contentView attribute:NSLayoutAttributeCenterY]; [self addConstraint:centerXConstraint]; [self addConstraint:centerYConstraint]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]]; // When a custom offset is available, we adjust the vertical constraints' constants if (self.verticalOffset != 0 && self.constraints.count > 0) { centerYConstraint.constant = self.verticalOffset; }
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; // Return any UIControl instance such as buttons, segmented controls, switches, etc. if ([hitView isKindOfClass:[UIControl class]]) { return hitView; } // Return either the contentView or customView if ([hitView isEqual:_contentView] || [hitView isEqual:_customView]) { return hitView; } return nil; }
靜態類結構框架