繁体   English   中英

在iOS中的折叠/展开表格视图中获取UiWebview的动态高度

[英]Get dynamic height of UiWebview in collapse/expand tableview in iOS

我需要获取每个单元格中每个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];
    }

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM