【问题标题】:Get dynamic height of UiWebview in collapse/expand tableview in iOS在iOS的折叠/展开tableview中获取UiWebview的动态高度
【发布时间】:2015-10-19 10:33:25
【问题描述】:

我需要获取每个单元格中每个 UiWebview 内容的高度,以便用户不必在 UiWebview 中滚动。但我不能用我当前的代码做到这一点。任何形式的帮助/建议都会非常有帮助。

#pragma mark Table View Delegate Methods starts        
    - (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 loadHTMLString:[NSString stringWithFormat:@"<div style='font-family:-apple-system','HelveticaNeue;'>%@",object.menuContent] baseURL:nil];
                //cell.contentWebView.scrollView.delegate = self;
                [cell.contentWebView.scrollView setShowsHorizontalScrollIndicator:YES];
                cell.contentWebView.scrollView.scrollEnabled = YES;
                //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
    {
        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
            {
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];
            }
        }
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //return UITableViewAutomaticDimension;
        if(indexPath.row == 0)
            return 50.00;
        else
        {
            return  150;//self.selectRowHeight;//[[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 Table View Delegate Method ends
    - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
        CGRect frame = aWebView.frame;
        frame.size.height = 1;
        aWebView.frame = frame;
        CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
        frame.size = fittingSize;
        //frame.origin.y = self.ContentImageView.frame.size.height + 10;
        aWebView.frame = frame;
        NSLog(@"height is %lf",aWebView.frame.size.height);
        //[self.imageHeightArray replaceObjectAtIndex:aWebView.tag - 1000 withObject:[NSString stringWithFormat:@"%lf",aWebView.frame.size.height]];
        self.selectRowHeight = aWebView.frame.size.height;
        [self.restaurantMenuTableView beginUpdates];
        [self.restaurantMenuTableView endUpdates];
        //    [self.restaurantMenuTableView reloadData];
    }

【问题讨论】:

    标签: ios uitableview uiwebview


    【解决方案1】:

    在您的情况下,您在 tableviewcell 中有 webview,因此在 heightForRowAtIndexPath 中将单元格的高度设置为 NSAttributedString 大小的高度,如下所示。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        NSAttributedString *attributedText = [NSString  getHTMLAttributedString:@"HTML Content"];
        CGSize size = CGSizeMake(300, CGFLOAT_MAX);
        CGRect paragraphRect =     [attributedText boundingRectWithSize:size
                                     options:(NSStringDrawingUsesLineFragmentOrigin)
                                     context:nil];
        return paragraphRect.size.height;
    }
    
    +(NSAttributedString *) getHTMLAttributedString:(NSString *) string{
        NSError *errorFees=nil;
        NSString *sourceFees = [NSString stringWithFormat:
                                @"<span style=\"font-family: 'Roboto-Light';font-size: 14px\">%@</span>",string];
        NSMutableAttributedString* strFees = [[NSMutableAttributedString alloc] initWithData:[sourceFees dataUsingEncoding:NSUTF8StringEncoding]
                                                                                     options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                               NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                                          documentAttributes:nil error:&errorFees];
        return strFees;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 2019-02-06
      • 2013-07-14
      • 2016-08-25
      • 2023-03-20
      • 1970-01-01
      • 2016-03-14
      相关资源
      最近更新 更多