Skip to content

Commit b1d4ddf

Browse files
committed
WKWebView + UITableView
1 parent eb47f38 commit b1d4ddf

File tree

5 files changed

+255
-35
lines changed

5 files changed

+255
-35
lines changed

iOS_Tips/DarkMode.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
78E179362351A40D0007E4BB /* SLEditMenuView.m in Sources */ = {isa = PBXBuildFile; fileRef = 78E179332351A40D0007E4BB /* SLEditMenuView.m */; };
124124
78E179372351A40D0007E4BB /* SLShotFocusView.m in Sources */ = {isa = PBXBuildFile; fileRef = 78E179342351A40D0007E4BB /* SLShotFocusView.m */; };
125125
78E72D5124766E4B00751373 /* SLWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 78E72D5024766E4B00751373 /* SLWebViewController.m */; };
126+
78E72D542477A4E700751373 /* SLWebTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 78E72D532477A4E700751373 /* SLWebTableViewController.m */; };
126127
78EF8B6024162B96008D0CD7 /* SLMenuViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 78EF8B5E24162B96008D0CD7 /* SLMenuViewController.m */; };
127128
78EF8B6124162B96008D0CD7 /* SLMenuViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 78EF8B5F24162B96008D0CD7 /* SLMenuViewController.xib */; };
128129
78EF8B6424188767008D0CD7 /* SLAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = 78EF8B6324188767008D0CD7 /* SLAlertView.m */; };
@@ -345,6 +346,8 @@
345346
78E179352351A40D0007E4BB /* SLEditMenuView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SLEditMenuView.h; sourceTree = "<group>"; };
346347
78E72D4F24766E4B00751373 /* SLWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SLWebViewController.h; sourceTree = "<group>"; };
347348
78E72D5024766E4B00751373 /* SLWebViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SLWebViewController.m; sourceTree = "<group>"; };
349+
78E72D522477A4E700751373 /* SLWebTableViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SLWebTableViewController.h; sourceTree = "<group>"; };
350+
78E72D532477A4E700751373 /* SLWebTableViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SLWebTableViewController.m; sourceTree = "<group>"; };
348351
78EF8B5D24162B96008D0CD7 /* SLMenuViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SLMenuViewController.h; sourceTree = "<group>"; };
349352
78EF8B5E24162B96008D0CD7 /* SLMenuViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SLMenuViewController.m; sourceTree = "<group>"; };
350353
78EF8B5F24162B96008D0CD7 /* SLMenuViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SLMenuViewController.xib; sourceTree = "<group>"; };
@@ -823,6 +826,8 @@
823826
children = (
824827
78E72D4F24766E4B00751373 /* SLWebViewController.h */,
825828
78E72D5024766E4B00751373 /* SLWebViewController.m */,
829+
78E72D522477A4E700751373 /* SLWebTableViewController.h */,
830+
78E72D532477A4E700751373 /* SLWebTableViewController.m */,
826831
);
827832
path = WKWebView;
828833
sourceTree = "<group>";
@@ -1135,6 +1140,7 @@
11351140
78E1791823517F700007E4BB /* SLImage.m in Sources */,
11361141
787C7DBE245D74E4005DF7ED /* NSMutableArray+SLCrashProtector.m in Sources */,
11371142
78A1998C2451BEE7005B2B4B /* UIButton+SLTitleImage.m in Sources */,
1143+
78E72D542477A4E700751373 /* SLWebTableViewController.m in Sources */,
11381144
783504642452C20B0071283E /* SLMethod.m in Sources */,
11391145
787C7E4524656524005DF7ED /* UIView+SLMLeakFinder.m in Sources */,
11401146
7862533F24599D420017F8F1 /* NSObject+SLCrashProtector.m in Sources */,

iOS_Tips/DarkMode/Crash/SLCrashProtector/NSObject+SLCrashProtector.m

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ - (SLKVODelegate *)KVODelegate {
102102

103103
// 添加监听者
104104
- (void)sl_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
105-
if (!IsSystemClass(self.class)) {
105+
// if (!IsSystemClass(self.class)) {
106106
objc_setAssociatedObject(self, KVODefenderKey, KVODefenderValue, OBJC_ASSOCIATION_RETAIN);
107107
if ([self.KVODelegate addInfoToMapWithObserver:observer forKeyPath:keyPath options:options context:context]) {
108108
// 如果添加 KVO 信息操作成功,则调用系统添加方法
@@ -114,13 +114,13 @@ - (void)sl_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath optio
114114
observer, keyPath, className];
115115
[[SLCrashHandler defaultCrashHandler] catchCrashException:nil type:SLCrashErrorTypeKVO errorDesc:errorReason];
116116
}
117-
} else {
118-
[self sl_addObserver:observer forKeyPath:keyPath options:options context:context];
119-
}
117+
// } else {
118+
// [self sl_addObserver:observer forKeyPath:keyPath options:options context:context];
119+
// }
120120
}
121121
// 移除监听者
122122
- (void)sl_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath{
123-
if (!IsSystemClass(self.class)) {
123+
// if (!IsSystemClass(self.class)) {
124124
if ([self.KVODelegate removeInfoInMapWithObserver:observer forKeyPath:keyPath]) {
125125
// 如果移除 KVO 信息操作成功,则调用系统移除方法
126126
[self sl_removeObserver:self.KVODelegate forKeyPath:keyPath];
@@ -130,13 +130,13 @@ - (void)sl_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath{
130130
NSString *errorReason = [NSString stringWithFormat:@"异常 KVO: Cannot remove an observer %@ for the key path '%@' from %@ , because it is not registered as an observer", observer, keyPath, className];
131131
[[SLCrashHandler defaultCrashHandler] catchCrashException:nil type:SLCrashErrorTypeKVO errorDesc:errorReason];
132132
}
133-
} else {
134-
[self sl_removeObserver:observer forKeyPath:keyPath];
135-
}
133+
// } else {
134+
// [self sl_removeObserver:observer forKeyPath:keyPath];
135+
// }
136136
}
137137
// 移除监听者
138138
- (void)sl_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(nullable void *)context{
139-
if (!IsSystemClass(self.class)) {
139+
// if (!IsSystemClass(self.class)) {
140140
if ([self.KVODelegate removeInfoInMapWithObserver:observer forKeyPath:keyPath context:context]) {
141141
// 如果移除 KVO 信息操作成功,则调用系统移除方法
142142
[self sl_removeObserver:self.KVODelegate forKeyPath:keyPath context:context];
@@ -146,10 +146,9 @@ - (void)sl_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath co
146146
NSString *errorReason = [NSString stringWithFormat:@"异常 KVO: Cannot remove an observer %@ for the key path '%@' from %@ , because it is not registered as an observer", observer, keyPath, className];
147147
[[SLCrashHandler defaultCrashHandler] catchCrashException:nil type:SLCrashErrorTypeKVO errorDesc:errorReason];
148148
}
149-
}
150-
else {
151-
[self sl_removeObserver:observer forKeyPath:keyPath context:context];
152-
}
149+
// } else {
150+
// [self sl_removeObserver:observer forKeyPath:keyPath context:context];
151+
// }
153152
}
154153
// 释放
155154
- (void)sl_KVODealloc{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// SLWebTableViewController.h
3+
// DarkMode
4+
//
5+
// Created by wsl on 2020/5/22.
6+
// Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
/*
14+
WKWebView + UITableView
15+
方案一:WebView作为TableView的Header,但不撑开webView
16+
*/
17+
@interface SLWebTableViewController : UIViewController
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//
2+
// SLWebTableViewController.m
3+
// DarkMode
4+
//
5+
// Created by wsl on 2020/5/22.
6+
// Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved.
7+
//
8+
9+
#import "SLWebTableViewController.h"
10+
#import <WebKit/WebKit.h>
11+
12+
@interface SLWebTableViewController ()<UITableViewDelegate,UITableViewDataSource>
13+
14+
@property (nonatomic, strong) WKWebView * webView;
15+
///网页加载进度视图
16+
@property (nonatomic, strong) UIProgressView * progressView;
17+
/// WKWebView 内容的高度
18+
@property (nonatomic, assign) CGFloat webContentHeight;
19+
20+
@property (nonatomic, strong) UITableView *tableView;
21+
22+
@end
23+
24+
@implementation SLWebTableViewController
25+
26+
- (void)viewDidLoad {
27+
[super viewDidLoad];
28+
[self setupUi];
29+
[self addKVO];
30+
}
31+
- (void)viewWillDisappear:(BOOL)animated {
32+
[super viewWillDisappear:animated];
33+
[self.progressView removeFromSuperview];
34+
}
35+
- (void)dealloc {
36+
[self removeKVO];
37+
NSLog(@"%@释放了",NSStringFromClass(self.class));
38+
}
39+
40+
#pragma mark - SetupUI
41+
- (void)setupUi {
42+
self.view.backgroundColor = [UIColor whiteColor];
43+
[self.view addSubview:self.tableView];
44+
[self configureWebTable];
45+
}
46+
- (void)configureWebTable {
47+
self.tableView.tableHeaderView = self.webView;
48+
self.tableView.bounces = NO;
49+
self.webView.scrollView.bounces = NO;
50+
}
51+
52+
#pragma mark - Getter
53+
- (UITableView *)tableView {
54+
if (_tableView == nil) {
55+
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) style:UITableViewStyleGrouped];
56+
_tableView.delegate = self;
57+
_tableView.dataSource = self;
58+
_tableView.estimatedRowHeight = 0;
59+
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
60+
}
61+
return _tableView;
62+
}
63+
- (WKWebView *)webView {
64+
if(_webView == nil){
65+
//创建网页配置
66+
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
67+
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) configuration:config];
68+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.jianshu.com/p/5cf0d241ae12"]];
69+
[_webView loadRequest:request];
70+
}
71+
return _webView;
72+
}
73+
- (UIProgressView *)progressView {
74+
if (!_progressView){
75+
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, 2)];
76+
_progressView.tintColor = [UIColor blueColor];
77+
_progressView.trackTintColor = [UIColor clearColor];
78+
}
79+
if (_progressView.superview == nil) {
80+
[self.navigationController.navigationBar addSubview:_progressView];
81+
}
82+
return _progressView;
83+
}
84+
85+
#pragma mark - KVO
86+
///添加键值对监听
87+
- (void)addKVO {
88+
//监听网页加载进度
89+
[self.webView addObserver:self
90+
forKeyPath:NSStringFromSelector(@selector(estimatedProgress))
91+
options:NSKeyValueObservingOptionNew
92+
context:nil];
93+
//监听网页内容高度
94+
[self.webView.scrollView addObserver:self
95+
forKeyPath:@"contentSize"
96+
options:NSKeyValueObservingOptionNew
97+
context:nil];
98+
}
99+
///移除监听
100+
- (void)removeKVO {
101+
//移除观察者
102+
[_webView removeObserver:self
103+
forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
104+
[_webView.scrollView removeObserver:self
105+
forKeyPath:NSStringFromSelector(@selector(contentSize))];
106+
}
107+
//kvo监听 必须实现此方法
108+
-(void)observeValueForKeyPath:(NSString *)keyPath
109+
ofObject:(id)object
110+
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
111+
context:(void *)context{
112+
113+
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))]
114+
&& object == _webView) {
115+
// NSLog(@"网页加载进度 = %f",_webView.estimatedProgress);
116+
self.progressView.progress = _webView.estimatedProgress;
117+
if (_webView.estimatedProgress >= 1.0f) {
118+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
119+
self.progressView.progress = 0;
120+
});
121+
}
122+
}else if ([keyPath isEqualToString:NSStringFromSelector(@selector(contentSize))]
123+
&& object == _webView.scrollView && _webContentHeight != _webView.scrollView.contentSize.height) {
124+
_webContentHeight = _webView.scrollView.contentSize.height;
125+
NSLog(@"WebViewContentSize = %@",NSStringFromCGSize(_webView.scrollView.contentSize))
126+
}
127+
}
128+
129+
#pragma mark - EventsHandle
130+
131+
#pragma mark - HelpMethods
132+
133+
#pragma mark - UITableViewDelegate,UITableViewDataSource
134+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
135+
return 1;
136+
}
137+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
138+
return 20;
139+
}
140+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
141+
return 80;
142+
}
143+
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
144+
return 44;
145+
}
146+
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
147+
UILabel *label = [UILabel new];
148+
label.text = @"评论";
149+
label.textColor = UIColor.whiteColor;
150+
label.textAlignment = NSTextAlignmentCenter;
151+
label.backgroundColor = [UIColor orangeColor];
152+
return label;
153+
}
154+
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
155+
return 0.1;
156+
}
157+
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
158+
return nil;
159+
}
160+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
161+
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
162+
cell.textLabel.text = [NSString stringWithFormat:@"%ld条评论",(long)indexPath.row];
163+
return cell;
164+
}
165+
166+
@end

0 commit comments

Comments
 (0)