ActivityIndicator for evey UIWebView in TableViewCell



我有一个表格视图,其中包含每个单元格中的网络视图。我想为每个单元格添加活动指示器。因此,每当 Web 视图完成加载时,该单元格的特定活动指示器都应停止动画。当然,我的代码中的网络视图委托不知道活动指示器。我该怎么做?感谢。

一些代码:

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

网页视图代表 :

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicator stopAnimating];
}

试试这个。

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

{ UITableViewCell *cell = [tableView dequeueReuseableCellWithIdentifier: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];
            }
    }
}

}

基本上,正如@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;
}

最新更新