ios - 在 iOS 中获取展开/折叠 UITableView 的动态 UIWebview 内容高度

标签 ios objective-c uitableview uiwebview

我在展开/折叠 UITableView 中加载 UIWebview。我需要根据 UIWebview 内容高度更改行高。如果我在 UITableview 中设置 UIWebview 委托(delegate),UIWebview delgate 方法会不断调用。我正在调用 webviewDidFinishLoad 方法来获取内容高度。任何形式的帮助/建议将不胜感激。

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{

    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.restauRantMenuArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    if ([self tableView:self.restaurantMenuTableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {

            return  2; // return rows when expanded
        }

        return 1; // only top row showing
    }

    // Return the number of rows in the section.
    return 1;


}



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


    if ([self tableView:self.restaurantMenuTableView canCollapseSection:indexPath.section])
    {
        if (indexPath.row == 0)
        {
            static NSString *MyIdentifier = @"menuTitleCell";

            RestaurantMenuTitleTableViewCell *cell =(RestaurantMenuTitleTableViewCell*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
            if (cell == nil) {
                cell = [[RestaurantMenuTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RestaurantMenuTitleTableViewCell" owner:self options:nil];
                cell = [topLevelObjects objectAtIndex:0];
            }


            RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];

            cell.titleWebView.opaque = NO;

            cell.titleWebView.backgroundColor = [UIColor clearColor];

            [cell.titleWebView loadHTMLString:object.menuTitle baseURL:nil];

            cell.titleWebView.scrollView.scrollEnabled = NO;


            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            cell.backgroundColor = [UIColor whiteColor];

            return cell;

        }
        else
        {
            static NSString *MyIdentifier = @"menuContentCell";

            RestaurantMenuContentTableViewCell *cell =(RestaurantMenuContentTableViewCell*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
            if (cell == nil) {
                cell = [[RestaurantMenuContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RestaurantMenuContentTableViewCell" owner:self options:nil];
                cell = [topLevelObjects objectAtIndex:0];
            }



            RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];



            cell.contentWebView.tag = indexPath.section + 1000;

            cell.contentWebView.opaque = NO;

            cell.contentWebView.backgroundColor = [UIColor clearColor];

            [cell.contentWebView loadHTMLString:object.menuContent baseURL:nil];

            //cell.contentWebView.scrollView.delegate = self;

            [cell.contentWebView.scrollView setShowsHorizontalScrollIndicator:NO];

           // cell.contentWebView.scrollView.scrollEnabled = NO;

            //cell.contentWebView.delegate = self;

            if([[self.imageHeightArray objectAtIndex:indexPath.section] isEqualToString:@"150"])
            {
                //cell.contentWebView.delegate = self;
            }


            // first row
            //cell.textLabel.text = @"Expandable"; // only top row showing

            if ([expandedSections containsIndex:indexPath.section])
            {
                //cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }
            else
            {
                //cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]
            }

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.backgroundColor = [UIColor clearColor];

            return cell;
        }
    }
    else
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.backgroundColor = [UIColor clearColor];

        return cell;

    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];

    UIWebView *thisWebView = (UIWebView*)[self.restaurantMenuTableView viewWithTag:indexPath.section+1000];

    [thisWebView loadHTMLString:object.menuContent baseURL:nil];

    thisWebView.delegate = self;



    //[tableView deselectRowAtIndexPath:indexPath animated:YES];

    //if there is any previous selected index
    //if selected index is not the previous selected

    if (selectedIndex && (selectedIndex.section != indexPath.section)) {
        [self collapseOrExpandForIndexPath:selectedIndex inTableView:tableView];
    }


    if ([selectedIndex isEqual:indexPath]) {
        selectedIndex = nil;
    }
    else{
        selectedIndex = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];

    }


    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        [self collapseOrExpandForIndexPath:indexPath inTableView:tableView];
        if (indexPath.row == 0) {
            [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }

    }

}

- (void)collapseOrExpandForIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView{

    if (!indexPath.row)
    {
        // only first row toggles exapand/collapse
        //[tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger section = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:section];
        NSInteger rows;

        NSMutableArray *tmpArray = [NSMutableArray array];

        if (currentlyExpanded)
        {
            rows = [self tableView:tableView numberOfRowsInSection:section];
            [expandedSections removeIndex:section];

        }
        else
        {
            [expandedSections addIndex:section];
            rows = [self tableView:tableView numberOfRowsInSection:section];
        }

        for (int i=1; i<rows; i++)
        {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                           inSection:section];
            [tmpArray addObject:tmpIndexPath];
        }


        if (currentlyExpanded)
        {
            [tableView deleteRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];



        }
        else
        {

            //RestaurantMenuItemObject *object = [self.restauRantMenuArray objectAtIndex:indexPath.section];

//            UIWebView *thisWebView = (UIWebView*)[self.restaurantMenuTableView viewWithTag:indexPath.section+1000];
//            
//            [thisWebView loadHTMLString:object.menuContent baseURL:nil];
//            
//            thisWebView.delegate = self;

            [tableView insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];


        }
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //return UITableViewAutomaticDimension;


    if(indexPath.row == 0)
        return 50.00;
    else
    {
        return 240;//[[self.imageHeightArray objectAtIndex:indexPath.section] integerValue];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.00;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.00;
}

pragma mark TableView 委托(delegate)方法结束

最佳答案

  1. 创建一个类级属性 (CGFloat),比如 webViewHeight 来跟踪 WebView 的高度。
  2. 在 table view didSelectRowAtIndexPath 方法中加载 web view 中的 url。
  3. 在 WebView 的 webviewDidFinishLoad 委托(delegate)方法中计算 WebView 高度,并将其设置在类级别的高度变量 webViewHeight 中。计算高度后调用[tableView reloadData];
  4. 使用 webViewHeight 设置选定的单元格高度,其余为默认值,例如数据源的 heightForRowAtIndexPath 方法中的 44.0。

IMP:如果计算高度超过最大高度,则也设置最大 Web View 高度,将计算高度设置为最大高度。并启用网页 View 滚动,以便用户能够查看所有网页 View 内容。

关于ios - 在 iOS 中获取展开/折叠 UITableView 的动态 UIWebview 内容高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010976/

相关文章:

objective-c - OpenGL透明纹理(混合)

ios - dequeueReusableHeaderFooterViewWithIdentifier 为单元格返回 nil,但 dequeueReusableCellWithIdentifier 不是

ios - 上传App到AppStore时设置只兼容iPad2和iPad3?

iphone - 用于从 Photos.app 上传图像的移动 safari 的 html5 网络应用程序?

objective-c - 无法将对象添加到 NSMutableArray

objective-c - MPMediaItem 的 URL

ios - UITableViewcell 中的弹出窗口不会立即出现。

Xcode 6.3 解析 SDK 1.7.1 PFTableViewCell 错误 "has incompatible type"

ios - CocoaPods:更新了 pre_install 语法?

iphone - cocos2d-x 应用程序的 HTTP 流量在 Charles 代理中不存在