0
votes

I´ve got a tableview which contains webviews in every cell. I´d like to add activity indicators for every cell. So whenever a webview didfinishload the specific activity indicator of that cell should stopanimating. Of course the webview delegate down in my code doesn´t know the activityIndicator. How can I do that? Thx.

Some code:

- (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];
}
2
did you tried something ? post you code here - Pawan Rai
i will suggest you to make a custom cell & implement the webview & its delegate in it. you can use the custom cell here, that will be better way to handle all this. - Pawan Rai
read tutorial before implementing it. Reference Link. there are so many tutorial related to how to make custom cell ? Good Luck. - Pawan Rai

2 Answers

1
votes

Try this.

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

}

0
votes

Basically as @pawan suggested, best way is to achieve this by CustomTableCell.

Meanwhile try this code:

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