0

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;
    }
Community
  • 1
  • 1
Viki
  • 421
  • 1
  • 4
  • 12
  • What do you mean by URLs are loading? Can you provide screenshots. – Hyder Jan 26 '16 at 09:39
  • I mean the urls are being generated which I've verified by `NSLog(@"%@", url);` Which screenshots do you want? – Viki Jan 26 '16 at 09:45
  • Post your code for your custom cell class, and add screenshots of iOS simulator se we can have an idea of whats showing and whats not – Hyder Jan 26 '16 at 10:18
  • I've done NOTHING in the custom cell class EXCEPT for binding an outlet of the webview in the interface section of .h file! – Viki Jan 26 '16 at 10:27

1 Answers1

0

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.

Hyder
  • 1,163
  • 2
  • 13
  • 37
  • I don't understand why it's not working. I've even tried adding a simple UIView and then used the tag like you've said, but they are not being displayed! – Viki Jan 26 '16 at 10:59
  • Can you please send me a sample file with just a `UIView` added to a tableViewCell using tags? – Viki Jan 26 '16 at 11:03
  • Sorry can't send you file at the moment. The `cell` creation is correct in the answer. There must be some issue with your tableView. Is your `tableView` connected properly? Are you doing `[self.tableView reloadData]` ? Bro, post screenshots of your storyboard , `.h` and `.m` files ! – Hyder Jan 26 '16 at 11:09
  • You didn't tell me to do `[self.tableView reloadData]`? I'll post them in a moment – Viki Jan 26 '16 at 11:13
  • we are not meant to spoonfeed you here! Somethings are obvious to do. Anyway reload your tableview in your `viewDidLoad`. – Hyder Jan 26 '16 at 11:14
  • 1. I'm using a subclass of `UITableViewController`. So the `tableView` is connected properly. 2. I don't know why I've to do `[self.tableView reloadData]` !Anyway it didn't work out ! 3. http://postimg.org/image/pc1d688gr and http://postimg.org/image/n672bq50b – Viki Jan 26 '16 at 11:21
  • 1. make sure your array has data. 2. call `[self.tableView reloadData]` in your `veiwDidLoad`. 3. add this line `aView.backgroundColor = [UIColor redColor];` before `return cell;` 4. Checkk your UI Constraints! – Hyder Jan 26 '16 at 11:28
  • Here is a zip file of what i've done. http://www.filehosting.org/file/details/538107/sampleFile.zip I've forgotten to tick `isInitialViewController` in the project. Please tick it and run the file It would be very helpful if you can tell me what's wrong! – Viki Jan 26 '16 at 11:43
  • Can you please do that or no? – Viki Jan 26 '16 at 12:04