cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/UIKitExtensions/UITableView+QMUI.m

614 lines
31 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// UITableView+QMUI.m
// qmui
//
// Created by QMUI Team on 15/7/20.
//
#import "UIView+QMUI.h"
#import "UITableView+QMUI.h"
#import "UITableViewCell+QMUI.h"
#import "QMUICore.h"
#import "UIScrollView+QMUI.h"
#import "QMUILog.h"
#import "NSObject+QMUI.h"
#import "CALayer+QMUI.h"
const NSUInteger kFloatValuePrecision = 4;//
@interface UITableView ()
@property(nonatomic, assign, readonly) CGRect qmui_indexFrame;
@end
@implementation UITableView (QMUI)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
OverrideImplementation([UITableView class], @selector(initWithFrame:style:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^UITableView *(UITableView *selfObject, CGRect firstArgv, UITableViewStyle secondArgv) {
// call super
UITableView *(*originSelectorIMP)(id, SEL, CGRect, UITableViewStyle);
originSelectorIMP = (UITableView * (*)(id, SEL, CGRect, UITableViewStyle))originalIMPProvider();
UITableView *result = originSelectorIMP(selfObject, originCMD, firstArgv, secondArgv);
// iOS 11 estimatedRowHeight UITableViewAutomaticDimensionestimate iOS 11 > 0
// 使 estimate contentSize 便 UITableView UIPickerTableView estimatedRowHeight QMUITableView init
// https://github.com/Tencent/QMUI_iOS/issues/313
if (QMUICMIActivated && [NSStringFromClass(selfObject.class) isEqualToString:@"UITableView"]) {
[selfObject _qmui_configEstimatedRowHeight];
}
return result;
};
});
OverrideImplementation([UITableView class], @selector(sizeThatFits:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^CGSize(UITableView *selfObject, CGSize size) {
[selfObject alertEstimatedHeightUsageIfDetected];
// call super
CGSize (*originSelectorIMP)(id, SEL, CGSize);
originSelectorIMP = (CGSize (*)(id, SEL, CGSize))originalIMPProvider();
CGSize result = originSelectorIMP(selfObject, originCMD, size);
return result;
};
});
OverrideImplementation([UITableView class], @selector(scrollToRowAtIndexPath:atScrollPosition:animated:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject, NSIndexPath *indexPath, UITableViewScrollPosition scrollPosition, BOOL animated) {
if (!indexPath) {
return;
}
BOOL isIndexPathLegal = YES;
NSInteger numberOfSections = [selfObject numberOfSections];
if (indexPath.section < 0 || indexPath.section >= numberOfSections) {
isIndexPathLegal = NO;
} else if (indexPath.row != NSNotFound) {
NSInteger rows = [selfObject numberOfRowsInSection:indexPath.section];
isIndexPathLegal = indexPath.row >= 0 && indexPath.row < rows;
}
if (!isIndexPathLegal) {
QMUIAssert(NO, @"UITableView (QMUI)", @"%@ - target indexPath : %@ 不合法的indexPath。\n%@", selfObject, indexPath, [NSThread callStackSymbols]);
return;
}
// call super
void (*originSelectorIMP)(id, SEL, NSIndexPath *, UITableViewScrollPosition, BOOL);
originSelectorIMP = (void (*)(id, SEL, NSIndexPath *, UITableViewScrollPosition, BOOL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, indexPath, scrollPosition, animated);
};
});
// [UIKit Bug] UISearchBar tableHeaderView 使 UITableView estimatedRowHeight contentSize
// https://github.com/Tencent/QMUI_iOS/issues/1161
void (^fixBugOfTableViewContentSize)(UITableView *) = ^void(UITableView *tableView) {
BOOL estimatesRowHeight = NO;
[tableView qmui_performSelector:NSSelectorFromString(@"_estimatesRowHeights") withPrimitiveReturnValue:&estimatesRowHeight];
if (estimatesRowHeight && [tableView.tableHeaderView isKindOfClass:UISearchBar.class]) {
BeginIgnorePerformSelectorLeaksWarning
[tableView performSelector:NSSelectorFromString(@"_updateContentSize")];
EndIgnorePerformSelectorLeaksWarning
}
};
/* - (void)_coalesceContentSizeUpdateWithDelta:(double)arg1; */
OverrideImplementation([UITableView class], NSSelectorFromString(@"_coalesceContentSizeUpdateWithDelta:"), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject, CGFloat firstArgv) {
// call super
void (*originSelectorIMP)(id, SEL, CGFloat);
originSelectorIMP = (void (*)(id, SEL, CGFloat))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, firstArgv);
if (fixBugOfTableViewContentSize) {
fixBugOfTableViewContentSize(selfObject);
}
};
});
OverrideImplementation([UITableView class], @selector(reloadData), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject) {
// [UIKit Bug] iOS 11 estimated height tableView crash
// https://github.com/Tencent/QMUI_iOS/issues/1243
if (![selfObject qmui_getBoundBOOLForKey:@"kHasCalledReloadDataOnce"] && [selfObject.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
NSInteger a = [selfObject.dataSource numberOfSectionsInTableView:selfObject];
NSInteger b = [selfObject numberOfSections];
if (a == 0 && b == 1) {
// - [UITableView noteNumberOfRowsChanged]
SEL selector = NSSelectorFromString([NSString qmui_stringByConcat:@"note", @"NumberOfRows", @"Changed", nil]);
if ([selfObject respondsToSelector:selector]) {
BeginIgnorePerformSelectorLeaksWarning
[selfObject performSelector:selector];
EndIgnorePerformSelectorLeaksWarning
}
}
}
// call super
void (*originSelectorIMP)(id, SEL);
originSelectorIMP = (void (*)(id, SEL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD);
// reloadData reloadData
[selfObject qmui_bindBOOL:YES forKey:@"kHasCalledReloadDataOnce"];
// [UIKit Bug] UISearchBar tableHeaderView 使 UITableView tableView window setTableHeaderView:reloadData
// https://github.com/Tencent/QMUI_iOS/issues/1215
// superview window UITableView UIViewController UITableViewController
if (!selfObject.window && selfObject.superview && [selfObject.tableHeaderView isKindOfClass:UISearchBar.class]) {
[selfObject qmui_bindBOOL:YES forKey:@"kShouldFixContentSizeBugKey"];
}
};
});
OverrideImplementation([UITableView class], @selector(didMoveToWindow), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject) {
// call super
void (*originSelectorIMP)(id, SEL);
originSelectorIMP = (void (*)(id, SEL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD);
if ([selfObject qmui_getBoundBOOLForKey:@"kShouldFixContentSizeBugKey"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[selfObject reloadData];
});
[selfObject qmui_bindBOOL:NO forKey:@"kShouldFixContentSizeBugKey"];
}
};
});
// [UIKit Bug] UISearchBar tableHeaderView 使 tableView sectionIndex searchBar
// https://github.com/Tencent/QMUI_iOS/issues/1213
OverrideImplementation([UITableView class], NSSelectorFromString(@"_removeIndex"), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject) {
// call super
void (*originSelectorIMP)(id, SEL);
originSelectorIMP = (void (*)(id, SEL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD);
UISearchBar *searchBar = (UISearchBar *)selfObject.tableHeaderView;
if ([searchBar isKindOfClass:UISearchBar.class]) {
// UISearchBar UITableView inset
[searchBar qmui_performSelector:NSSelectorFromString(@"_updateInsetsForTableView:") withArguments:&selfObject, nil];
}
};
});
});
}
// release indexPath crash
- (void)qmui_scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
if (!indexPath) {
return;
}
BOOL isIndexPathLegal = YES;
NSInteger numberOfSections = [self numberOfSections];
if (indexPath.section >= numberOfSections) {
isIndexPathLegal = NO;
} else if (indexPath.row != NSNotFound) {
NSInteger rows = [self numberOfRowsInSection:indexPath.section];
isIndexPathLegal = indexPath.row < rows;
}
if (!isIndexPathLegal) {
QMUIAssert(NO, @"UITableView (QMUI)", @"%@ - target indexPath : %@ 不合法的indexPath。\n%@", self, indexPath, [NSThread callStackSymbols]);
} else {
[self qmui_scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
}
- (void)qmui_styledAsQMUITableView {
if (!QMUICMIActivated) return;
[self _qmui_configEstimatedRowHeight];
self.backgroundColor = PreferredValueForTableViewStyle(self.style, TableViewBackgroundColor, TableViewGroupedBackgroundColor, TableViewInsetGroupedBackgroundColor);
self.separatorColor = PreferredValueForTableViewStyle(self.style, TableViewSeparatorColor, TableViewGroupedSeparatorColor, TableViewInsetGroupedSeparatorColor);
// cell
if (self.style == UITableViewStylePlain) {
self.tableFooterView = [[UIView alloc] init];
}
self.backgroundView = [[UIView alloc] init]; // backgroundView使 backgroundColor tableHeaderView UISearchBar 使 backgroundView
self.sectionIndexColor = TableSectionIndexColor;
self.sectionIndexTrackingBackgroundColor = TableSectionIndexTrackingBackgroundColor;
self.sectionIndexBackgroundColor = TableSectionIndexBackgroundColor;
#ifdef IOS15_SDK_ALLOWED
if (@available(iOS 15.0, *)) {
self.sectionHeaderTopPadding = PreferredValueForTableViewStyle(self.style, TableViewSectionHeaderTopPadding, TableViewGroupedSectionHeaderTopPadding, TableViewInsetGroupedSectionHeaderTopPadding);
}
#endif
self.qmui_insetGroupedCornerRadius = TableViewInsetGroupedCornerRadius;
self.qmui_insetGroupedHorizontalInset = TableViewInsetGroupedHorizontalInset;
}
- (void)_qmui_configEstimatedRowHeight {
if (TableViewEstimatedHeightEnabled) {
self.estimatedRowHeight = TableViewCellNormalHeight;
self.rowHeight = UITableViewAutomaticDimension;
self.estimatedSectionHeaderHeight = UITableViewAutomaticDimension;
self.sectionHeaderHeight = UITableViewAutomaticDimension;
self.estimatedSectionFooterHeight = UITableViewAutomaticDimension;
self.sectionFooterHeight = UITableViewAutomaticDimension;
} else {
self.estimatedRowHeight = 0;
self.rowHeight = TableViewCellNormalHeight;
self.estimatedSectionHeaderHeight = 0;
self.sectionHeaderHeight = UITableViewAutomaticDimension;
self.estimatedSectionFooterHeight = 0;
self.sectionFooterHeight = UITableViewAutomaticDimension;
}
}
- (NSIndexPath *)qmui_indexPathForRowAtView:(UIView *)view {
if (!view || !view.superview) {
return nil;
}
if ([view isKindOfClass:[UITableViewCell class]] && ([NSStringFromClass(view.superview.class) isEqualToString:@"UITableViewWrapperView"] ? view.superview.superview : view.superview) == self) {
// iOS 11 cell.superview UITableViewiOS 11 cell.superview UITableViewWrapperView
return [self indexPathForCell:(UITableViewCell *)view];
}
return [self qmui_indexPathForRowAtView:view.superview];
}
- (NSInteger)qmui_indexForSectionHeaderAtView:(UIView *)view {
[self alertEstimatedHeightUsageIfDetected];
if (!view || ![view isKindOfClass:[UIView class]]) {
return -1;
}
CGPoint origin = [self convertPoint:view.frame.origin fromView:view.superview];
origin = CGPointToFixed(origin, kFloatValuePrecision);//
NSInteger low = 0;
NSInteger high = [self numberOfSections];
while (low <= high) {
NSInteger mid = low + ((high-low) >> 1);
CGRect rectForSection = [self rectForSection:mid];
rectForSection = CGRectToFixed(rectForSection, kFloatValuePrecision);
if (CGRectContainsPoint(rectForSection, origin)) {
UITableViewHeaderFooterView *headerView = [self headerViewForSection:mid];
if (headerView && [view isDescendantOfView:headerView]) {
return mid;
} else {
return -1;
}
} else if (rectForSection.origin.y < origin.y) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
- (NSArray<NSNumber *> *)qmui_indexForVisibleSectionHeaders {
// iOS 14 section header section cell visibleRows iOS 15 cell visibleRows
// iOS 15 cell section cell section
NSMutableArray<NSNumber *> *result = NSMutableArray.new;
NSInteger sections = self.numberOfSections;
for (NSInteger section = 0; section < sections; section++) {
if ([self qmui_isHeaderVisibleForSection:section]) {
[result addObject:@(section)];
}
}
if (result.count == 0) {
result = nil;
}
return result;
}
- (NSInteger)qmui_indexOfPinnedSectionHeader {
NSArray<NSNumber *> *visibleSectionIndex = [self qmui_indexForVisibleSectionHeaders];
for (NSInteger i = 0; i < visibleSectionIndex.count; i++) {
NSInteger section = visibleSectionIndex[i].integerValue;
if ([self qmui_isHeaderPinnedForSection:section]) {
return section;
} else {
continue;
}
}
return -1;
}
- (BOOL)qmui_isHeaderPinnedForSection:(NSInteger)section {
if (self.style != UITableViewStylePlain) return NO;
if (section >= [self numberOfSections]) return NO;
// rect contentSize rect rect
CGRect rectForSection = [self rectForSection:section];
CGRect rectForHeader = [self rectForHeaderInSection:section];
BOOL isSectionScrollIntoContentInsetTop = self.contentOffset.y + self.adjustedContentInset.top > CGRectGetMinY(rectForHeader);// section contentInset.top 线
BOOL isSectionStayInContentInsetTop = self.contentOffset.y + self.adjustedContentInset.top <= CGRectGetMaxY(rectForSection) - CGRectGetHeight(rectForHeader);// section
BOOL isPinned = isSectionScrollIntoContentInsetTop && isSectionStayInContentInsetTop;
return isPinned;
}
- (BOOL)qmui_isHeaderVisibleForSection:(NSInteger)section {
if (section >= [self numberOfSections]) return NO;
// header
CGRect rectForSectionHeader = [self rectForHeaderInSection:section];
if (CGRectGetHeight(rectForSectionHeader) <= 0) return NO;
// rect contentSize rect rect
CGRect rectForSection = CGRectZero;
if (self.style == UITableViewStylePlain) {
rectForSection = [self rectForSection:section];
} else {
rectForSection = [self rectForHeaderInSection:section];
}
CGRect visibleRect = CGRectMake(self.contentOffset.x + self.adjustedContentInset.left, self.contentOffset.y + self.adjustedContentInset.top, CGRectGetWidth(self.bounds) - UIEdgeInsetsGetHorizontalValue(self.adjustedContentInset), CGRectGetHeight(self.bounds) - UIEdgeInsetsGetVerticalValue(self.adjustedContentInset));
if (CGRectIntersectsRect(visibleRect, rectForSection)) {
return YES;
}
return NO;
}
- (QMUITableViewCellPosition)qmui_positionForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger numberOfRowsInSection = [self.dataSource tableView:self numberOfRowsInSection:indexPath.section];
if (numberOfRowsInSection == 1) {
return QMUITableViewCellPositionSingleInSection;
}
if (indexPath.row == 0) {
return QMUITableViewCellPositionFirstInSection;
}
if (indexPath.row == numberOfRowsInSection - 1) {
return QMUITableViewCellPositionLastInSection;
}
return QMUITableViewCellPositionMiddleInSection;
}
- (BOOL)qmui_cellVisibleAtIndexPath:(NSIndexPath *)indexPath {
NSArray<NSIndexPath *> *visibleCellIndexPaths = self.indexPathsForVisibleRows;
for (NSIndexPath *visibleIndexPath in visibleCellIndexPaths) {
if ([indexPath isEqual:visibleIndexPath]) {
return YES;
}
}
return NO;
}
- (void)qmui_clearsSelection {
NSArray<NSIndexPath *> *selectedIndexPaths = [self indexPathsForSelectedRows];
for (NSIndexPath *indexPath in selectedIndexPaths) {
[self deselectRowAtIndexPath:indexPath animated:YES];
}
}
- (void)qmui_scrollToRowFittingOffsetY:(CGFloat)offsetY atIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
[self alertEstimatedHeightUsageIfDetected];
if (![self qmui_canScroll]) {
return;
}
CGRect rectForRow = [self rectForRowAtIndexPath:indexPath];
if (CGRectEqualToRect(rectForRow, CGRectZero)) {
return;
}
// rowrow
BOOL canScrollRowToTop = CGRectGetMaxY(rectForRow) + CGRectGetHeight(self.frame) - (offsetY + CGRectGetHeight(rectForRow)) <= self.contentSize.height;
if (canScrollRowToTop) {
[self setContentOffset:CGPointMake(self.contentOffset.x, CGRectGetMinY(rectForRow) - offsetY) animated:animated];
} else {
[self qmui_scrollToBottomAnimated:animated];
}
}
- (CGFloat)qmui_validContentWidth {
CGRect indexFrame = self.qmui_indexFrame;
CGFloat rightInset = MAX(self.safeAreaInsets.right + (self.style == UITableViewStyleInsetGrouped ? self.qmui_insetGroupedHorizontalInset : 0), CGRectGetWidth(indexFrame));
CGFloat leftInset = self.safeAreaInsets.left + (self.style == UITableViewStyleInsetGrouped ? self.qmui_insetGroupedHorizontalInset : 0);
CGFloat width = CGRectGetWidth(self.bounds) - leftInset - rightInset;
return width;
}
- (CGSize)qmui_realContentSize {
[self alertEstimatedHeightUsageIfDetected];
if (!self.dataSource || !self.delegate) {
return CGSizeZero;
}
CGSize contentSize = self.contentSize;
CGFloat footerViewMaxY = CGRectGetMaxY(self.tableFooterView.frame);
CGSize realContentSize = CGSizeMake(contentSize.width, footerViewMaxY);
NSInteger lastSection = [self numberOfSections] - 1;
if (lastSection < 0) {
// numberOfSetions0tableViewcellfooterView
return realContentSize;
}
CGRect lastSectionRect = [self rectForSection:lastSection];
realContentSize.height = fmax(realContentSize.height, CGRectGetMaxY(lastSectionRect));
return realContentSize;
}
- (BOOL)qmui_canScroll {
//
if (CGRectGetHeight(self.bounds) <= 0) {
return NO;
}
if ([self.tableHeaderView isKindOfClass:[UISearchBar class]]) {
BOOL canScroll = self.qmui_realContentSize.height + UIEdgeInsetsGetVerticalValue(self.adjustedContentInset) > CGRectGetHeight(self.bounds);
return canScroll;
} else {
return [super qmui_canScroll];
}
}
- (void)alertEstimatedHeightUsageIfDetected {
BOOL usingEstimatedRowHeight = [self.delegate respondsToSelector:@selector(tableView:estimatedHeightForRowAtIndexPath:)] || self.estimatedRowHeight > 0;
BOOL usingEstimatedSectionHeaderHeight = [self.delegate respondsToSelector:@selector(tableView:estimatedHeightForHeaderInSection:)] || self.estimatedSectionHeaderHeight > 0;
BOOL usingEstimatedSectionFooterHeight = [self.delegate respondsToSelector:@selector(tableView:estimatedHeightForFooterInSection:)] || self.estimatedSectionFooterHeight > 0;
if (usingEstimatedRowHeight || usingEstimatedSectionHeaderHeight || usingEstimatedSectionFooterHeight) {
[self QMUISymbolicUsingTableViewEstimatedHeightMakeWarning];
}
}
- (void)QMUISymbolicUsingTableViewEstimatedHeightMakeWarning {
QMUILog(@"UITableView (QMUI)", @"当开启了 UITableView 的 estimatedRow(SectionHeader / SectionFooter)Height 功能后,不应该手动修改 contentOffset 和 contentSize也会影响 contentSize、sizeThatFits:、rectForXxx 等方法的计算,请注意确认当前是否存在不合理的业务代码。可添加 '%@' 的 Symbolic Breakpoint 以捕捉此类信息\n%@", NSStringFromSelector(_cmd), [NSThread callStackSymbols]);
}
- (void)qmui_performBatchUpdates:(void (NS_NOESCAPE ^ _Nullable)(void))updates completion:(void (^ _Nullable)(BOOL finished))completion {
[self performBatchUpdates:updates completion:completion];
}
- (CGRect)qmui_indexFrame {
CGRect indexFrame = CGRectZero;
[self qmui_performSelector:NSSelectorFromString(@"indexFrame") withPrimitiveReturnValue:&indexFrame];
return indexFrame;
}
@end
@interface UITableViewCell (QMUI_Private)
@property(nonatomic, assign, readwrite) QMUITableViewCellPosition qmui_cellPosition;
@end
@implementation UITableView (QMUI_InsetGrouped)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// -[UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath:] delegate
OverrideImplementation([UITableView class], NSSelectorFromString(@"_configureCellForDisplay:forIndexPath:"), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableView *selfObject, UITableViewCell *cell, NSIndexPath *indexPath) {
// call super
void (*originSelectorIMP)(id, SEL, UITableViewCell *, NSIndexPath *);
originSelectorIMP = (void (*)(id, SEL, UITableViewCell *, NSIndexPath *))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, cell, indexPath);
// UITableViewCell(QMUI) cellPosition separator 使
QMUITableViewCellPosition position = [selfObject qmui_positionForRowAtIndexPath:indexPath];
cell.qmui_cellPosition = position;
if (selfObject.style == UITableViewStyleInsetGrouped) {
CGFloat cornerRadius = selfObject.qmui_insetGroupedCornerRadius;
if (position == QMUITableViewCellPositionMiddleInSection || position == QMUITableViewCellPositionNone) {
cornerRadius = 0;
}
cell.layer.cornerRadius = cornerRadius;
}
if (cell.qmui_configureStyleBlock) {
cell.qmui_configureStyleBlock(selfObject, cell, indexPath);
}
};
});
// -[UITableViewCell _setContentClipCorners:updateCorners:] InsetGrouped cell.backgroundColor... iOS 12 -[UITableView _configureCellForDisplay:forIndexPath:]
// - (void) _setContentClipCorners:(unsigned long)arg1 updateCorners:(BOOL)arg2; (0x10db0a5b7)
OverrideImplementation([UITableViewCell class], NSSelectorFromString([NSString qmui_stringByConcat:@"_setContentClipCorners", @":", @"updateCorners", @":", nil]), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITableViewCell *selfObject, CACornerMask firstArgv, BOOL secondArgv) {
// call super
void (*originSelectorIMP)(id, SEL, CACornerMask, BOOL);
originSelectorIMP = (void (*)(id, SEL, CACornerMask, BOOL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, firstArgv, secondArgv);
UITableView *tableView = selfObject.qmui_tableView;
if (tableView && tableView.style == UITableViewStyleInsetGrouped) {
CGFloat cornerRadius = tableView.qmui_insetGroupedCornerRadius;
if (selfObject.qmui_cellPosition == QMUITableViewCellPositionMiddleInSection || selfObject.qmui_cellPosition == QMUITableViewCellPositionNone) {
cornerRadius = 0;
}
selfObject.layer.cornerRadius = cornerRadius;
}
};
});
// -[UITableView layoutMargins] InsetGrouped
OverrideImplementation([UITableView class], @selector(layoutMargins), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^UIEdgeInsets(UITableView *selfObject) {
// call super
UIEdgeInsets (*originSelectorIMP)(id, SEL);
originSelectorIMP = (UIEdgeInsets (*)(id, SEL))originalIMPProvider();
UIEdgeInsets result = originSelectorIMP(selfObject, originCMD);
if (selfObject.style == UITableViewStyleInsetGrouped) {
result.left = selfObject.safeAreaInsets.left + selfObject.qmui_insetGroupedHorizontalInset;
result.right = selfObject.safeAreaInsets.right + selfObject.qmui_insetGroupedHorizontalInset;
}
return result;
};
});
});
}
static char kAssociatedObjectKey_insetGroupedCornerRadius;
- (void)setQmui_insetGroupedCornerRadius:(CGFloat)qmui_insetGroupedCornerRadius {
objc_setAssociatedObject(self, &kAssociatedObjectKey_insetGroupedCornerRadius, @(qmui_insetGroupedCornerRadius), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (self.style == UITableViewStyleInsetGrouped && self.indexPathsForVisibleRows.count) {
[self reloadData];
}
}
- (CGFloat)qmui_insetGroupedCornerRadius {
NSNumber *associatedValue = (NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_insetGroupedCornerRadius);
if (!associatedValue) {
// UIAppearance iOS 13
// UITableView init 使 UIAppearance
return 10;
}
return associatedValue.qmui_CGFloatValue;
}
static char kAssociatedObjectKey_insetGroupedHorizontalInset;
- (void)setQmui_insetGroupedHorizontalInset:(CGFloat)qmui_insetGroupedHorizontalInset {
objc_setAssociatedObject(self, &kAssociatedObjectKey_insetGroupedHorizontalInset, @(qmui_insetGroupedHorizontalInset), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (self.style == UITableViewStyleInsetGrouped && self.indexPathsForVisibleRows.count) {
[self reloadData];
}
}
- (CGFloat)qmui_insetGroupedHorizontalInset {
NSNumber *associatedValue = (NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_insetGroupedHorizontalInset);
if (!associatedValue) {
// UIAppearance iOS 13
// UITableView init 使 UIAppearance
return PreferredValueForVisualDevice(20, 15);
}
return associatedValue.qmui_CGFloatValue;
}
@end