aLFaRSi
aLFaRSi

Reputation: 559

Loading an html files to a web view

I have a table view with a lot of items when i click on a cell in the table it loads a view with a web view outlet, in the web view in viewdidload i have this line of code to load the html file wich is in the project file

NSString *name = pagename.text;
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:@"html"]isDirectory:NO]]];

the code to push from the selected cell to that web view with is called Test is

if (indexPath.row == 0) 
{

    Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
    test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:test animated:YES];

    NSString *name1 = @"ba";
    [[test pagename] setText:[NSString stringWithString:(NSString *)name1]];

}

else if (indexPath.row == 1) 
{

    Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
    test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:test animated:YES];

    NSString *name1 = @"bb";
    [[test pagename] setText:[NSString stringWithString:(NSString *)name1]];

}

else if (indexPath.row == 2) 
{

    Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
    test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:test animated:YES];

    NSString *name1 = @"bc";
    [[test pagename] setText:[NSString stringWithString:(NSString *)name1]];

}

the html files are called ba,bb,bc and bd .html

the problem is when i select any cell of the four cells i have in the table it shows the same html file "ba.html"

i tried everything , cleaning the project, deleting the files and copying them back, changed the simulator, restarted the lap and even changed the files names.

any help ?

thanks in advance

update :

This is the hole code

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array count];
}


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

    static NSString *CellIdentifier = @"CustomCell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [[cell textLabel] setTextAlignment:UITextAlignmentRight];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.textLabel.numberOfLines = 3;

    return cell;
}

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0) 
    {

        Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
        test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:test animated:YES];

        NSString *name1 = @"ba";
        [[test pagename] setText:name1];

    }

    else if (indexPath.row == 1) 
    {

        Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
        test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:test animated:YES];

        NSString *name1 = @"bb";
        [[test pagename] setText:name1];        
    }

    else if (indexPath.row == 2) 
    {

        Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
        test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:test animated:YES];

        NSString *name1 = @"bc";
        [[test pagename] setText:name1];
    }

    else if (indexPath.row == 3) 
    {

        Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
        test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:test animated:YES];

        NSString *name1 = @"bd";
        [[test pagename] setText:name1];

    }
}

Upvotes: 0

Views: 137

Answers (2)

You are presenting the controller before setting the page.

Make 'name' an NSString instance variable in your webview class. Make it a property (nonatomic,retain) and synthesize it.

Instead of setting the viewcontroller's label, set name. Then in viewDidLoad, set the label from name.

Set the label from the name, not the other way around.

In the cell was selected:

Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSString *name1 = @"bc";
[test setName:[NSString stringWithString:(NSString *)name1]];
[self presentModalViewController:test animated:YES];

In the Web view viewdidload

-(void)viewDidLoad {
    pagename.text = self.name;
    [WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:@"html"]isDirectory:NO]]];
}

Upvotes: 1

J_D
J_D

Reputation: 3596

Can you put a breakpoint or a log to see if indexPath.row is always 0?

If this is the case, then maybe your table is created in a way that each element is in its own section. You would then have to use indexPath.section as each object would be row 0 of its own section.

Also, I do not know which type of object is 'pagename' but setters generally retain the parameters, so you do not have to create a copy of name1 in

[[test pagename] setText:[NSString stringWithString:(NSString *)name1]];

Note: this really depends on how the property is declared in the class, but typically, those type of properties are retained.

Upvotes: 0

Related Questions