Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2011, 10:06 AM   #1 (permalink)
Registered Member
 
Ogispert's Avatar
 
Join Date: May 2011
Posts: 49
Ogispert is on a distinguished road
Default How to open a TextView within a TableView populated from a .plist

Hi everyone.

Let's see if I can explain myself.

I have some table views that I've populated with info from a .plist file. There I have no problems, since all the rows and subviews look fine.

At the end, the object is that each row has to open a different .txt file in a text view.

I've tried to do the same thing that I did in another project where the table view was populated from an array:

Code:
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:textSelected ofType:@"txt"];
    if (filepath) {
        NSString *myText = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:NULL];  
        if (myText) {  
            Article.text = myText;  
        }  
    }
}
Sadly, it isn't working.

The problem is that now each row opens the same .txt file (The first one of the list/bundle).

Also I have some rows in the last table view that have the same name, so how can they open a different file?

Ex. of my .plist
Rows
-item 0
---Children
-----Item 0
-------Title 1A (Should open file1.txt)
---Title 1
-Item 1
---Children
-----Item 0
-------Title 1A (Should open file2.txt)
---Title 2

Do I have to use some code like “If, else”?

Could you help me please?

Thank’s in advance.

Last edited by Ogispert; 05-27-2011 at 10:32 AM. Reason: Change title
Ogispert is offline   Reply With Quote
Old 05-27-2011, 10:31 AM   #2 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Post the method where you choose what txt file to open.

Quote:
Originally Posted by Ogispert View Post
Hi everyone.

Let's see if I can explain myself.

I have some table views that I've populated with info from a .plist file. There I have no problems, since all the rows and subviews look fine.

At the end, the object is that each row has to open a different .txt file in a text view.

I've tried to do the same thing that I did in another project where the table view was populated from an array:

Code:
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:textSelected ofType:@"txt"];
    if (filepath) {
        NSString *myText = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:NULL];  
        if (myText) {  
            Article.text = myText;  
        }  
    }
}
Sadly, it isn't working.

The problem is that now each row opens the same .txt file (The first one of the list/bundle).

Also I have some rows in the last table view that have the same name, so how can they open a different file?

Ex. of my .plist
Rows
-item 0
---Children
-----Item 0
-------Title 1A (Should open file1.txt)
---Title 1
-Item 1
---Children
-----Item 0
-------Title 1A (Should open file2.txt)
---Title 2

Do I have to use some code like “If, else”?

Could you help me please?

Thank’s in advance.
iSDK is offline   Reply With Quote
Old 05-27-2011, 10:38 AM   #3 (permalink)
Registered Member
 
Ogispert's Avatar
 
Join Date: May 2011
Posts: 49
Ogispert is on a distinguished road
Default

Quote:
Originally Posted by iSDK View Post
Post the method where you choose what txt file to open.
This is my complete TextViewController:

Code:
@implementation ArticleViewController

@synthesize Article;
@synthesize textSelected;

-(id) initWithTextSelected:(NSString *) text {
    
    self.textSelected = text;
    [Article setText:@" @"];
    return self;
}

- (void)dealloc
{
    [textSelected release];
    [Article release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [Article setText:[self textSelected]];
    self.title = @"";
    
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:textSelected ofType:@"txt"];
    if (filepath) {
        NSString *myText = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:NULL];  
        if (myText) {  
            Article.text = myText;  
        }  
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.Article = nil;
}

@end
An this is the .h file

Code:
@interface ArticleViewController : UIViewController
{
    IBOutlet UITextView *Article;
    NSString *textSelected;

}

@property (nonatomic, retain) UITextView *Article;
@property (nonatomic, retain) NSString *textSelected;

-(id) initWithTextSelected:(NSString *) text;

@end
Ogispert is offline   Reply With Quote
Old 05-27-2011, 10:42 AM   #4 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Firstly, I don't think the initWithTextSelected method would work as you are not actually initialising 'self.

Code:
-(id) initWithTextSelected:(NSString *) text {
    self = [super init];
    if (self) {
        self.textSelected = text;
        [Article setText:@" @"];
    }
    return self;
}
Secondly, where do you decide what the name of the .txt file that you are going to read is?


Quote:
Originally Posted by Ogispert View Post
This is my complete TextViewController:

Code:
@implementation ArticleViewController

@synthesize Article;
@synthesize textSelected;

-(id) initWithTextSelected:(NSString *) text {
    
    self.textSelected = text;
    [Article setText:@" @"];
    return self;
}

- (void)dealloc
{
    [textSelected release];
    [Article release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [Article setText:[self textSelected]];
    self.title = @"";
    
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:textSelected ofType:@"txt"];
    if (filepath) {
        NSString *myText = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:NULL];  
        if (myText) {  
            Article.text = myText;  
        }  
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.Article = nil;
}

@end
An this is the .h file

Code:
@interface ArticleViewController : UIViewController
{
    IBOutlet UITextView *Article;
    NSString *textSelected;

}

@property (nonatomic, retain) UITextView *Article;
@property (nonatomic, retain) NSString *textSelected;

-(id) initWithTextSelected:(NSString *) text;

@end
iSDK is offline   Reply With Quote
Old 05-27-2011, 10:55 AM   #5 (permalink)
Registered Member
 
Ogispert's Avatar
 
Join Date: May 2011
Posts: 49
Ogispert is on a distinguished road
Default

Quote:
Originally Posted by iSDK View Post
Firstly, I don't think the initWithTextSelected method would work as you are not actually initialising 'self.

Code:
-(id) initWithTextSelected:(NSString *) text {
    self = [super init];
    if (self) {
        self.textSelected = text;
        [Article setText:@" @"];
    }
    return self;
}
Secondly, where do you decide what the name of the .txt file that you are going to read is?

In my other project, the name of the file was the same of the string, here i tried to do the same. I have no other name.

This is declared in my TableViewController:

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
	NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
	cell.textLabel.text = [dictionary objectForKey:@"Title"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
	
	NSArray *Children = [dictionary objectForKey:@"Children"];
	
	if([Children count] == 0) {
		
		ArticleViewController *dvController = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:[NSBundle mainBundle]];
		[self.navigationController pushViewController:dvController animated:YES];
		[dvController release];
	}
	else {
		
		RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
		
		rvController.CurrentLevel += 1;
		
		rvController.CurrentTitle = [dictionary objectForKey:@"Title"];
		
		[self.navigationController pushViewController:rvController animated:YES];
		
		rvController.tableDataSource = Children;
		
		[rvController release];
	}
}
Ogispert is offline   Reply With Quote
Old 05-27-2011, 12:20 PM   #6 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 8
taylortm is on a distinguished road
Default Your code points to the same .txt file every time

Quote:
Originally Posted by Ogispert View Post
Hi everyone.

At the end, the object is that each row has to open a different .txt file in a text view.

I've tried to do the same thing that I did in another project where the table view was populated from an array:

Code:
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:textSelected ofType:@"txt"];
    if (filepath) {
        NSString *myText = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:NULL];  
        if (myText) {  
            Article.text = myText;  
        }  
    }
}
Sadly, it isn't working.

The problem is that now each row opens the same .txt file (The first one of the list/bundle).
From looking at your code, it seems like you're basically just saying:

Open the file pointed to (named by?) the variable textSelected. My guess is that you are not properly updating textSelected.

Which view controller is this code from? The Tableview? The Table Cell?
taylortm is offline   Reply With Quote
Old 05-27-2011, 12:30 PM   #7 (permalink)
Registered Member
 
Ogispert's Avatar
 
Join Date: May 2011
Posts: 49
Ogispert is on a distinguished road
Default

Quote:
Originally Posted by taylortm View Post
From looking at your code, it seems like you're basically just saying:

Open the file pointed to (named by?) the variable textSelected. My guess is that you are not properly updating textSelected.

Which view controller is this code from? The Tableview? The Table Cell?
It comes from a table view, and is declared at the ViewController.

Another approach I'm trying is calling the .txt file from the .plist file (inserting a reference as a sting, like the titles), but again, I don't know the right code for that...
Ogispert is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 337
6 members and 331 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0