作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表格 View ,其中包含每个单元格中的 WebView 。我想为每个单元格添加事件指示器。因此,只要 webview 完成加载,该单元格的特定事件指示器就应该停止动画。当然,我代码中的 webview 委托(delegate)不知道 activityIndicator。我怎样才能做到这一点?谢谢。
部分代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
if (cell == nil) {
NSLog(@"creating a new cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]autorelease];
[tableView setRowHeight:screenHeight-15];
UIWebView *webview=[[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight-95)]autorelease];
webview.delegate = self;
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
[cell.contentView addSubview:webview];
UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator.center = CGPointMake(screenWidth/2, (screenHeight/2)-50);
[activityIndicator startAnimating];
[cell.contentView addSubview:activityIndicator];
}
return cell;
}
WebView 委托(delegate):
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
最佳答案
试试这个。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
if (cell == nil) {
NSLog(@"creating a new cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]autorelease];
[tableView setRowHeight:screenHeight-15];
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight-95)];
webview.delegate = self;
[webview setTag:indexPath.row];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webview loadRequest:nsrequest];
UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator.center = CGPointMake(webview.frame.size.width/2, (webview.frame.size.height/2)-50);
[activityIndicator setTag:500];
[activityIndicator startAnimating];
[webview addSubview:activityIndicator];
[cell.contentView addSubview:webview];
}
return cell;
}- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSArray *arrSub=[[NSArray alloc]initWithArray:webView.subviews];
for (int i=0; i<[arrSub count]; i++) {
UIView *viewChild=[arrSub objectAtIndex:i];
if (viewChild.tag==500) {
if ([viewChild isKindOfClass:[UIActivityIndicatorView class]]) {
UIActivityIndicatorView *activityIndicator=(UIActivityIndicatorView*)viewChild;
[activityIndicator stopAnimating];
}
}
}
关于ios - UITableViewCell 中每个 WebView 的 ActivityIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27562906/
我是一名优秀的程序员,十分优秀!