How to Get webview from UITableView didselectrowatindexpath?

℡╲_俬逩灬. 提交于 2019-12-13 07:33:00

问题


I have a UITableview in this view i created uiwebview for each cell. When i tap on a cell i want the view of UIWebView. How to achieve this??

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

        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

        }
        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        cell.accessoryType = UITableViewCellAccessoryNone;


        UIView *customView = [[UIView alloc]init];
        customView.transform = CGAffineTransformMakeRotation(M_PI_2);
        [customView setFrame:CGRectMake(0, 0, 1024, 768)];

        [customView setBackgroundColor:[UIColor clearColor]];

        UIWebView *menuWeb = [[UIWebView alloc]init];
        [menuWeb setFrame:CGRectMake(0, 0, 768, 1024)];
        [menuWeb setDelegate:self];
        [menuWeb setUserInteractionEnabled:YES];
        [menuWeb.scrollView setScrollEnabled:NO];
        [menuWeb setScalesPageToFit:YES];

        NSURL *url = [NSURL fileURLWithPath:[urlArr objectAtIndex:indexPath.row]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [menuWeb loadRequest:request];
        [customView addSubview:menuWeb];
        [cell.contentView addSubview:customView];
        [cell setBackgroundColor:[UIColor clearColor]];

        return cell;

    }

回答1:


in cellForRowAtIndexPath:

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

     ....
     UIView *customView = [[UIView alloc]init];
     customView.tag = 100;
     ....
     return cell;
}

and in

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

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      UIView *customView = [cell.contentView viewWithTag:100];

      for (id view in customView.subViews) {
           if ([view isKindOfClass:[UIWebView class]]) {
                UIWebView *menuWeb = (UIWebView *)view;
           }
      }
 }

hope this will help




回答2:


There are a couple of ways you can achieve this. The simplest way would be to assign a tag to the webview and then retreive it when the cell is tapped.

When you create the cell add:

[menuWeb setTag:SOME_CONSTANT_NUMBER];

Then inside the webview delegate's didSelectRowAtIndexPath method retrieve the cell and it's webView:

UITableViewCell *cell = [tableVoew cellForRowAtIndexPath:indexPath];
UIWebView *webView = [cell viewWithTag:SOME_CONSTANT_NUMBER]; 



回答3:


Try this one, in cellForRowAtIndexPath tag CustomView and webview so that you can retrieve using tag-

......
customView.tag = 111;
......
webView.tag = 222;
.......

Then in didSelectRowAtIndexPath

UITableViewCell *cell = (UITableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
UIView *customView = [cell.contentView viewWithTag:111];

Use viewWithTag to find out webview

UIWebView *wView = [customView viewWithTag:222];

Or iterate over custom view to find out webview

UIWebView *wView = nil;
for ((id) subView in [customView subviews]) {
        if ([subView isKindOfClass:[UIWebView class]]){
            //Found WebView
            wView =(UIWebView *) subView;

            break;
        }
}


来源:https://stackoverflow.com/questions/22322728/how-to-get-webview-from-uitableview-didselectrowatindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!