2014-12-19 91 views
0

我有一個tableview,其中包含每個單元格中的webviews。我想爲每個單元格添加活動指示器。所以,無論何時webview確實有效,該單元的特定活動指示器應該停止動作。當然,我的代碼中的webview代理不會知道activityIndi​​cator。我怎樣才能做到這一點?謝謝。TableViewCell中evey UIWebView的ActivityIndi​​cator

一些代碼:

- (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委託:

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 

    [activityIndicator stopAnimating]; 
} 
+0

做你嘗試過什麼?在這裏發佈你的代碼 – Pawan 2014-12-19 09:24:39

+0

我添加了一些代碼。 – tyler 2014-12-19 09:36:13

+1

我會建議你製作一個自定義單元格並在其中實現webview及其代理。你可以在這裏使用自定義單元格,這將是處理所有這些問題的更好方法。 – Pawan 2014-12-19 09:39:45

回答

1

試試這個。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 的UITableViewCell *細胞= [的tableView dequeueReusableCellWithIdentifier:無];

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; 

} - (空)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]; 
      } 
    } 
} 

}

0

基本上是以@pawan建議,最好的辦法是通過CustomTableCell實現這一目標。

同時試試這個代碼:

- (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; 

NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 

[webview loadRequest:nsrequest]; 

[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; 
} 
相關問題