如何在完成多个 UIWebView 请求后在 UITableViewCell 中找到 activity 指示符?

How do I find an activity indicator in a UITableViewCell on completion of multiple UIWebView requests?

我对 Objective-C 和 iOS 编程还很陌生。

我有一个 SummaryUITableViewCell(继承自 UITableViewCell 的自定义 class),其中包含一个 Activity 指示器(loadingSpinner)和一个 UIWebView(webView).

我的应用获取要加载的 URL 列表,然后显示 table 视图,每个 URL.

一个单元格

cellForRowAtIndexPath 中,我启动加载微调器的动画并调用 cell.webView loadRequest:URL

一切正常,每个 URL 调用一次 webViewDidFinishLoad(目前它只有一个 NSLog 语句)。我想不通的是如何找到合适的 loadingSpinner 以便我可以停止动画并隐藏它。

您希望每个 SummaryUITableViewCell 实现 UIWebViewDelegate 并自行处理 webViewDidFinishLoad 调用。然后,您可以在每次 UIWebView 加载时轻松隐藏微调器。这是您可以实现 SummaryUITableViewCell.

的一种方法

SummaryTableViewCell.h

#import <UIKit/UIKit.h>

@interface SummaryTableViewCell : UITableViewCell <UIWebViewDelegate>

@end

SummaryTableViewCell.m

#import "SummaryTableViewCell.h"

@interface SummaryTableViewCell ()

// Keep references to our spinner and webview here
@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation SummaryTableViewCell

- (instancetype)initWithUrl:(NSString *)url {
    self = [super init];
    if (self) {
        [self setup:url];
    }
    return self;
}

- (void)setup:(NSString *)url {
    // Add Webview
    self.webView = [[UIWebView alloc] initWithFrame:[self frame]];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    [self.webView setAlpha:0];

    // Set the cell as the delegate of the webview
    [self.webView setDelegate:self];
    [self addSubview:self.webView];

    // Add Spinner
    self.spinner = [[UIActivityIndicatorView alloc] init];
    [self addSubview:self.spinner];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // The web view loaded the url so we can now hide the spinner and show the web view
    [self.spinner setAlpha:0];
    [self.webView setAlpha:1];
}

@end