如何从UITableView didselectrowatindexpath获取webview?

时间:2014-03-11 10:18:55

标签: ios objective-c uitableview uiwebview

我在这个视图中有一个UITableview,我为每个单元创建了uiwebview。当我点击一个单元格时,我想要UIWebView的视图。如何实现这个??

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

    }

3 个答案:

答案 0 :(得分:1)

cellForRowAtIndexPath:

中的

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

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

并在

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

希望这会有所帮助

答案 1 :(得分:0)

尝试这个,在cellForRowAtIndexPath标记中使用CustomView和webview,以便您可以使用标记进行检索 -

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

然后在didSelectRowAtIndexPath

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

使用viewWithTag查找webview

UIWebView *wView = [customView viewWithTag:222];

或迭代自定义视图以查找webview

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

            break;
        }
}

答案 2 :(得分:0)

有几种方法可以实现这一目标。 最简单的方法是将标签分配给webview,然后在点击单元格时对其进行检索。

创建单元格时添加:

[menuWeb setTag:SOME_CONSTANT_NUMBER];

然后在webview委托的didSelectRowAtIndexPath方法中检索单元格并且它是webView:

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