UITableViewCell中每个UIWebView的活动指示器

时间:2014-12-19 09:21:15

标签: ios objective-c ios8

我有一个tableview,其中包含每个单元格中的webview。我想为每个细胞添加活动指标。因此,每当webview完成装载时,该单元的特定活动指示器应该停止动画。当然,我的代码中的webview委托不知道activityIndi​​cator。我怎样才能做到这一点? THX。

一些代码:

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

2 个答案:

答案 0 :(得分:1)

试试这个。

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

}

答案 1 :(得分: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;
}