简体   繁体   中英

UIWebView not being displayed in UITableViewCell - iOS

In the following code, webTableViewCell is a subclass of UITableViewCell (which I've created to avoid this ) in which I've binded an IBOutlet of webview from a prototype cell of a tableView in my viewController to this webTableViewCell class.

The URLs are loading whereas the webViews in the tableViewCell aren't. I think the problem is within the tableView:cellForRowAtIndexPath: method. Can someone help me out?

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        webTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"My Cell"];

if(!cell)
cell = [[webTableViewCell alloc]initWithFrame:CGRectZero];


    NSString *urlString = [NSString stringWithFormat:@"http://www.facebook.com/%@",[myArrayOfLinks objectAtIndex:indexPath.row]];
    NSURL *url = [NSURL URLWithString:urlString];
            NSLog(@"%@", url);


NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[cell.webView loadRequest:urlRequest];

return cell;
    }

Okay, no need to make a custom cell class.

do the following:

  1. Change your cell class back to UITableViewCell in the Storyboard and make its Reuse Identifier as "MyCell" (without spaces).

  2. Drag a webview and put it in your prototype cell in the storyboard (I guess you have already done this part), and assign it the tag value as 999 .

  3. In your cellForRowAtIndexPath method, do the following:

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath]; NSString *urlString = [NSString stringWithFormat:@"http://www.facebook.com/%@",[myArrayOfLinks objectAtIndex:indexPath.row]]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; UIWebView *webView = [cell viewWithTag:999]; [webView loadRequest:urlRequest]; return cell; 

run your app now and check. this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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