Skip to content

Commit dc5f162

Browse files
committed
cell加载webView调整高度的bug
1 parent 64c4aa2 commit dc5f162

4 files changed

Lines changed: 65 additions & 54 deletions

File tree

Coding_iOS/Controllers/CodeViewController.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "WebContentManager.h"
1212

1313
@interface CodeViewController ()
14-
@property (strong, nonatomic) UIWebView *codeContentView;
14+
@property (strong, nonatomic) UIWebView *webContentView;
1515
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
1616

1717
@end
@@ -31,20 +31,20 @@ - (void)viewDidLoad {
3131

3232
{
3333
//用webView显示内容
34-
_codeContentView = [[UIWebView alloc] initWithFrame:self.view.bounds];
35-
_codeContentView.delegate = self;
36-
_codeContentView.backgroundColor = [UIColor clearColor];
37-
_codeContentView.opaque = NO;
38-
_codeContentView.scalesPageToFit = YES;
39-
[self.view addSubview:_codeContentView];
34+
_webContentView = [[UIWebView alloc] initWithFrame:self.view.bounds];
35+
_webContentView.delegate = self;
36+
_webContentView.backgroundColor = [UIColor clearColor];
37+
_webContentView.opaque = NO;
38+
_webContentView.scalesPageToFit = YES;
39+
[self.view addSubview:_webContentView];
4040
//webview加载指示
4141
_activityIndicator = [[UIActivityIndicatorView alloc]
4242
initWithActivityIndicatorStyle:
4343
UIActivityIndicatorViewStyleGray];
4444
_activityIndicator.hidesWhenStopped = YES;
45-
[_activityIndicator setCenter:CGPointMake(CGRectGetWidth(_codeContentView.frame)/2, CGRectGetHeight(_codeContentView.frame)/2)];
46-
[_codeContentView addSubview:_activityIndicator];
47-
[_codeContentView mas_makeConstraints:^(MASConstraintMaker *make) {
45+
[_activityIndicator setCenter:CGPointMake(CGRectGetWidth(_webContentView.frame)/2, CGRectGetHeight(_webContentView.frame)/2)];
46+
[_webContentView addSubview:_activityIndicator];
47+
[_webContentView mas_makeConstraints:^(MASConstraintMaker *make) {
4848
make.edges.equalTo(self.view);
4949
}];
5050
}
@@ -86,10 +86,10 @@ - (void)refreshCodeViewData{
8686
if ([_myCodeFile.file.mode isEqualToString:@"image"]) {
8787
NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@u/%@/p/%@/git/raw/%@/%@", kNetPath_Code_Base, _myProject.owner_user_name, _myProject.name, _myCodeFile.ref, _myCodeFile.file.path]];
8888
NSLog(@"imageUrl: %@", imageUrl);
89-
[self.codeContentView loadRequest:[NSURLRequest requestWithURL:imageUrl]];
89+
[self.webContentView loadRequest:[NSURLRequest requestWithURL:imageUrl]];
9090
}else if ([_myCodeFile.file.mode isEqualToString:@"file"]){
9191
NSString *contentStr = [WebContentManager codePatternedWithContent:_myCodeFile];
92-
[self.codeContentView loadHTMLString:contentStr baseURL:nil];
92+
[self.webContentView loadHTMLString:contentStr baseURL:nil];
9393
}
9494
}
9595

Coding_iOS/Views/Cell/ProjectReadMeCell.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ - (void)setCurProject:(Project *)curProject{
7171
return;
7272
}
7373
[_webContentView setHeight:_curProject.readMeHeight];
74-
[_activityIndicator startAnimating];
75-
NSString *webDataStr = _curProject.readMeHtml? _curProject.readMeHtml: @"";
76-
[self.webContentView loadHTMLString:[WebContentManager markdownPatternedWithContent:webDataStr] baseURL:nil];
74+
if (!_webContentView.isLoading) {
75+
[_activityIndicator startAnimating];
76+
if (_curProject.readMeHtml) {
77+
[self.webContentView loadHTMLString:[WebContentManager markdownPatternedWithContent:_curProject.readMeHtml] baseURL:nil];
78+
}
79+
}
7780
}
7881
+ (CGFloat)cellHeightWithObj:(id)obj{
7982
CGFloat cellHeight = 0;

Coding_iOS/Views/Cell/TopicContentCell.m

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ @interface TopicContentCell ()
1717
@property (strong, nonatomic) UIImageView *userIconView;
1818
@property (strong, nonatomic) UILabel *titleLabel, *timeLabel, *commentCountLabel;
1919
@property (strong, nonatomic) UIButton *commentBtn, *deleteBtn;
20-
@property (strong, nonatomic) UIWebView *topicContentView;
20+
@property (strong, nonatomic) UIWebView *webContentView;
2121
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
2222
@end
2323
@implementation TopicContentCell
@@ -48,15 +48,15 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
4848
[self.contentView addSubview:_timeLabel];
4949
}
5050
curWidth = kScreen_Width - 2*kPaddingLeftWidth;
51-
if (!self.topicContentView) {
52-
self.topicContentView = [[UIWebView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, 0, curWidth, 1)];
53-
self.topicContentView.delegate = self;
54-
self.topicContentView.scrollView.scrollEnabled = NO;
55-
self.topicContentView.scrollView.scrollsToTop = NO;
56-
self.topicContentView.scrollView.bounces = NO;
57-
self.topicContentView.backgroundColor = [UIColor clearColor];
58-
self.topicContentView.opaque = NO;
59-
[self.contentView addSubview:self.topicContentView];
51+
if (!self.webContentView) {
52+
self.webContentView = [[UIWebView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, 0, curWidth, 1)];
53+
self.webContentView.delegate = self;
54+
self.webContentView.scrollView.scrollEnabled = NO;
55+
self.webContentView.scrollView.scrollsToTop = NO;
56+
self.webContentView.scrollView.bounces = NO;
57+
self.webContentView.backgroundColor = [UIColor clearColor];
58+
self.webContentView.opaque = NO;
59+
[self.contentView addSubview:self.webContentView];
6060
}
6161
if (!_activityIndicator) {
6262
_activityIndicator = [[UIActivityIndicatorView alloc]
@@ -112,11 +112,15 @@ - (void)setCurTopic:(ProjectTopic *)curTopic{
112112
curBottomY += 10+ 20;
113113

114114
// 讨论的内容
115-
[self.topicContentView setY:curBottomY];
116-
[self.topicContentView setHeight:_curTopic.contentHeight];
117-
[_activityIndicator startAnimating];
118-
[self.topicContentView loadHTMLString:[WebContentManager topicPatternedWithContent:_curTopic.htmlMedia.contentOrigional] baseURL:nil];
119-
[_activityIndicator setCenter:CGPointMake(kScreen_Width/2, curBottomY+5)];
115+
[self.webContentView setY:curBottomY];
116+
[self.webContentView setHeight:_curTopic.contentHeight];
117+
118+
if (!_webContentView.isLoading) {
119+
[_activityIndicator startAnimating];
120+
if (_curTopic.htmlMedia.contentOrigional) {
121+
[self.webContentView loadHTMLString:[WebContentManager markdownPatternedWithContent:_curTopic.htmlMedia.contentOrigional] baseURL:nil];
122+
}
123+
}
120124

121125
curBottomY += _curTopic.contentHeight+5;
122126
[_commentCountLabel setY:curBottomY+2];
@@ -160,7 +164,7 @@ - (void)webViewDidStartLoad:(UIWebView *)webView{
160164
[_activityIndicator startAnimating];
161165
}
162166
- (void)webViewDidFinishLoad:(UIWebView *)webView{
163-
[self refreshtopicContentView];
167+
[self refreshwebContentView];
164168
[_activityIndicator stopAnimating];
165169
CGFloat scrollHeight = webView.scrollView.contentSize.height;
166170
if (ABS(scrollHeight - _curTopic.contentHeight) > 5) {
@@ -179,13 +183,13 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
179183
DebugLog(@"%@", error.description);
180184
}
181185

182-
- (void)refreshtopicContentView{
183-
if (_topicContentView) {
186+
- (void)refreshwebContentView{
187+
if (_webContentView) {
184188
// NSString *js = @"window.onload = function(){ document.body.style.backgroundColor = '#333333';}";
185-
// [_topicContentView stringByEvaluatingJavaScriptFromString:js];
189+
// [_webContentView stringByEvaluatingJavaScriptFromString:js];
186190
//修改服务器页面的meta的值
187-
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", CGRectGetWidth(_topicContentView.frame)];
188-
[_topicContentView stringByEvaluatingJavaScriptFromString:meta];
191+
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", CGRectGetWidth(_webContentView.frame)];
192+
[_webContentView stringByEvaluatingJavaScriptFromString:meta];
189193
}
190194
}
191195
#pragma mark Btn M

Coding_iOS/Views/Cell/TweetDetailCell.m

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ @interface TweetDetailCell ()
3535
@property (strong, nonatomic) UIButton *likeBtn, *commentBtn, *deleteBtn;
3636
@property (strong, nonatomic) UICustomCollectionView *likeUsersView;
3737
@property (strong, nonatomic) UIImageView *timeClockIconView;
38-
@property (strong, nonatomic) UIWebView *tweetContentView;
38+
@property (strong, nonatomic) UIWebView *webContentView;
3939
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
4040

4141
@end
@@ -118,15 +118,15 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
118118
[self.likeUsersView addLineUp:YES andDown:NO andColor:[UIColor colorWithHexString:@"0xdddddd"]];
119119
[self.contentView addSubview:self.likeUsersView];
120120
}
121-
if (!self.tweetContentView) {
122-
self.tweetContentView = [[UIWebView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, kTweetDetailCell_PadingTop, kTweetDetailCell_ContentWidth, 1)];
123-
self.tweetContentView.delegate = self;
124-
self.tweetContentView.scrollView.scrollEnabled = NO;
125-
self.tweetContentView.scrollView.scrollsToTop = NO;
126-
self.tweetContentView.scrollView.bounces = NO;
127-
self.tweetContentView.backgroundColor = [UIColor clearColor];
128-
self.tweetContentView.opaque = NO;
129-
[self.contentView addSubview:self.tweetContentView];
121+
if (!self.webContentView) {
122+
self.webContentView = [[UIWebView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, kTweetDetailCell_PadingTop, kTweetDetailCell_ContentWidth, 1)];
123+
self.webContentView.delegate = self;
124+
self.webContentView.scrollView.scrollEnabled = NO;
125+
self.webContentView.scrollView.scrollsToTop = NO;
126+
self.webContentView.scrollView.bounces = NO;
127+
self.webContentView.backgroundColor = [UIColor clearColor];
128+
self.webContentView.opaque = NO;
129+
[self.contentView addSubview:self.webContentView];
130130
}
131131
if (!_activityIndicator) {
132132
_activityIndicator = [[UIActivityIndicatorView alloc]
@@ -174,9 +174,13 @@ - (void)setTweet:(Tweet *)tweet{
174174
[self.timeClockIconView setX:timeLabelX-15];
175175

176176
//owner冒泡text内容
177-
[self.tweetContentView setHeight:_tweet.contentHeight];
178-
[_activityIndicator startAnimating];
179-
[self.tweetContentView loadHTMLString:[WebContentManager bubblePatternedWithContent:_tweet.htmlMedia.contentOrigional] baseURL:nil];
177+
[self.webContentView setHeight:_tweet.contentHeight];
178+
if (!_webContentView.isLoading) {
179+
[_activityIndicator startAnimating];
180+
if (_tweet.htmlMedia.contentOrigional) {
181+
[self.webContentView loadHTMLString:[WebContentManager markdownPatternedWithContent:_tweet.htmlMedia.contentOrigional] baseURL:nil];
182+
}
183+
}
180184

181185
CGFloat curBottomY = kTweetDetailCell_PadingTop +[[self class] contentHeightWithTweet:_tweet];
182186
curBottomY += 10;
@@ -271,7 +275,7 @@ - (void)webViewDidStartLoad:(UIWebView *)webView{
271275
[_activityIndicator startAnimating];
272276
}
273277
- (void)webViewDidFinishLoad:(UIWebView *)webView{
274-
[self refreshTweetContentView];
278+
[self refreshwebContentView];
275279
[_activityIndicator stopAnimating];
276280
CGFloat scrollHeight = webView.scrollView.contentSize.height;
277281
if (ABS(scrollHeight - _tweet.contentHeight) > 5) {
@@ -291,11 +295,11 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
291295
}
292296

293297

294-
- (void)refreshTweetContentView{
295-
if (_tweetContentView) {
298+
- (void)refreshwebContentView{
299+
if (_webContentView) {
296300
//修改服务器页面的meta的值
297-
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", CGRectGetWidth(_tweetContentView.frame)];
298-
[_tweetContentView stringByEvaluatingJavaScriptFromString:meta];
301+
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", CGRectGetWidth(_webContentView.frame)];
302+
[_webContentView stringByEvaluatingJavaScriptFromString:meta];
299303
}
300304
}
301305

0 commit comments

Comments
 (0)