iOS在UIWebViews之间切换导致内存警告

时间:2012-10-26 13:48:25

标签: ios uiwebview webview

您好我创建了一个自定义标签栏,隐藏并显示某些Web视图,具体取决于您当前正在加载操作队列上的所有三个Web视图的哪个选项卡,然后根据所选选项卡使它们可见且不可见。问题是加载和滚动这么慢,我收到了一个收到的内存警告

继续我到目前为止所做的事情

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSArray *subviews = [cell subviews];
        for(UIView *subview in subviews) if([subview tag] == 4) [subview removeFromSuperview];

        UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
        UIView *sendingMessage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        UIActivityIndicatorView *sendingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(25, 15, 50, 50)];
        UILabel *sendingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 100, 20)];

        int sendingViewLeft = (loadingView.frame.size.width/2) - (sendingMessage.frame.size.width/2);
        int sendingViewTop = 160 - (sendingMessage.frame.size.height/2);

        loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

        sendingMessage.frame = CGRectMake(sendingViewLeft, sendingViewTop, sendingMessage.frame.size.width, sendingMessage.frame.size.height);
        sendingMessage.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        sendingMessage.layer.cornerRadius = 10;

        sendingSpinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

        sendingLabel.textColor = [UIColor whiteColor];
        sendingLabel.backgroundColor = [UIColor clearColor];
        sendingLabel.text = @"Loading...";
        sendingLabel.textAlignment = UITextAlignmentCenter;
        sendingLabel.font = [UIFont boldSystemFontOfSize:12];
        sendingLabel.shadowColor = [UIColor blackColor];
        sendingLabel.shadowOffset = CGSizeMake(0, -1);

        [sendingSpinner startAnimating];
        [sendingMessage addSubview:sendingLabel];
        [sendingMessage addSubview:sendingSpinner];
        [loadingView addSubview:sendingMessage];

        [loadingView setTag:4];

       cell.textLabel.text = @"";


    if([self.webViewStatus isEqualToString:@"FALSE"]){
        [cell addSubview:loadingView];
        self.tableView.userInteractionEnabled = NO;
    } else {
        [cell addSubview:self.fixturesWebView];
        [cell addSubview:self.resultsWebView];
        [cell addSubview:self.tablesWebView];
        self.tableView.userInteractionEnabled = YES;
    }


    if(self.selectedTabNumber == 1){
        self.fixturesWebView.alpha = 1;
        self.resultsWebView.alpha = 0;
        self.tablesWebView.alpha = 0;
    }

    if(self.selectedTabNumber == 2){
        self.fixturesWebView.alpha = 0;
        self.resultsWebView.alpha = 1;
        self.tablesWebView.alpha = 0;
    }

    if(self.selectedTabNumber == 3){
        self.fixturesWebView.alpha = 0;
        self.resultsWebView.alpha = 0;
        self.tablesWebView.alpha = 1;

    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

首先,您应该考虑创建自定义单元格,而不是在tableview数据源中创建它。其次,当您获取webview内容时,请考虑使用异步调用以避免锁定。这也可以在一个块中完成。如果您在自定义单元格上编写委托方法,则可以让表格视图知道何时加载内容等。

但是当tableview需要绘制单元格时,看起来你正在做太多工作。