单击特定的 UITableView 行后如何显示 WebView 数据?

How to Display WebView data after clicking a particular UITableView row?

在 table 视图中有很多行,当我单击任何 table 视图行时,它会像上面的例子一样展开。

所以,我想通过这种方式显示一些其他数据,我不想导航到下一个视图。

还有一点是,当它展开时单击特定行后,我想在这里显示网络视图。在我的项目中,一些数据是一些文本行的消息或者可能是一个网页。 因此,它还必须显示网页视图,并且必须根据数据或网页内容动态调整 table 视图单元格。

像这样,如果单击任何单元格,它必须展开并显示网络视图。即使有时它不包含任何网络数据,也可能包含一些消息,那么它必须显示网络视图。

这是我当前的项目示例。我正在显示 data/information 以下方式。 这是我的 table 视图(见图 1),点击行后它会显示一个像这样的弹出窗口(见图 2)- 它包含一些消息,所以像这样显示,图 3 - 它包含网络数据/网络视图.

如果有人知道解决方案,请post解决方案。谢谢..!

这是我的 didSelectRowAtIndexPath 方法,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *finaldic=[mutableArray objectAtIndex:indexPath.row];
    [self showWebview:@"" body:[finaldic objectForKey:@"body"] popupStyle:CNPPopupStyleActionSheet];
}

和 WebView 方法,

-(void)showWebview:(NSString*)tittle body:(NSString*)body popupStyle:(CNPPopupStyle)popupStyle{

    NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;

    NSAttributedString *title = [[NSAttributedString alloc] initWithString:tittle attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:14], NSParagraphStyleAttributeName : paragraphStyle}];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.attributedText = title;

    //    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    //    customView.backgroundColor = [UIColor hx_colorWithHexString:@"#00aeef"];
    //     [customView addSubview:titleLabel];

    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height/2)];
    // webview.scalesPageToFit = YES;
    webview.autoresizesSubviews = YES;
    //webview.delegate=self;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

    NSRange range = [body rangeOfString:@"<body"];

    if(range.location != NSNotFound) {
        // Adjust style for mobile
        float inset = 40;
        NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
        body = [NSString stringWithFormat:@"%@%@%@", [body substringToIndex:range.location], style, [body substringFromIndex:range.location]];
    }
    [webview loadHTMLString:body baseURL:nil];


    self.popupController = [[CNPPopupController alloc] initWithContents:@[titleLabel,webview]];
    self.popupController.theme = [CNPPopupTheme defaultTheme];
    self.popupController.theme.popupStyle = popupStyle;
    self.popupController.delegate = self;
    [self.popupController presentPopupControllerAnimated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

}
-(void)webViewDidStartLoad:(UIWebView *)webView{

}

设计两个不同的 table 视图单元格很简单。一个是简单的,另一个是扩展单元格。当您单击简单单元格时,然后将特定单元格更改为展开单元格。

检查我下面的代码,它会帮助你..!我根据你的代码创建的。

这是您的 didSelectRowAtIndexPath 方法,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{



    //user taps expnmade view

    if(selectedIndex == indexPath.row)
    {

        selectedIndex =-1;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]  withRowAnimation:UITableViewRowAnimationFade ];
        return;
    }

    //user taps diff row
    if(selectedIndex != -1)
    {

        NSIndexPath *prevPath= [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        selectedIndex=(int)indexPath.row;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevPath, nil]  withRowAnimation:UITableViewRowAnimationFade ];
    }


    //user taps new row with none expanded
    selectedIndex =(int)indexPath.row;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]  withRowAnimation:UITableViewRowAnimationFade ];

}

写这个方法,

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

    if(selectedIndex == indexPath.row)
    {
        // return  200;

        UITableViewCell   *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath];
        return cell.bounds.size.height;
    }
    else
    {
        return  90;
    }
}

并在 cellForRowAtIndexPath 方法中,添加此代码

NSDictionary *finaldic=[mutableArray objectAtIndex:indexPath.row];



    NSString *body= [finaldic objectForKey:@"body"];  
    NSRange range = [body rangeOfString:@"<body"];

    if(range.location != NSNotFound) {
        // Adjust style for mobile
        float inset = 40;
        NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
        body = [NSString stringWithFormat:@"%@%@%@", [body substringToIndex:range.location], style, [body substringFromIndex:range.location]];
    }
    [cell.webView loadHTMLString:body baseURL:nil];

我希望这对你有用..!