I'm going to ask this question again,I thought I had figured it out but I guess not. This is for a timer app that gets the timer info from a plist file. Everything works but I want the .plist extension not to show in my tableview. Here are two code snippets that I believe are the issue. These are in an array called NSArray *files;
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
// Configure the cell.
cell.textLabel.text = [[files objectAtIndex:indexPath.row] lastPathComponent];
return cell;
Thanks for the help
NSString *text = [[files objectAtIndex:indexPath.row] lastPathComponent];
text = [text substringToIndex:[text rangeOfString:#"."].location];
cell.textLabel.text = text;
Related
How to display the created file name and contents on textview? This is my code. Iam able to display the file name and its contents in NSLog but not on textview though i have given the outlet for textview. Please help me with this issue.
-(IBAction) open: (id) sender
{
// Create instance of UITextView
textView = [UITextView alloc];
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString *homeDir = NSHomeDirectory();
// Add text
textView.text= homeDir;
NSLog(#"%#", homeDir);
textfield.text= #"output will go here..";
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:#"Myfile"
ofType:#"txt"];
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSString *s = [NSString stringWithFormat:#"myFilePath: %#, Contents of file:%#",myFilePath, myFileContents];
NSLog(#"%#", s);
textView.text= s;
}
Does the textView work at all? Have you tried:
textView.text = #"TestText"? If the textView remains empty you need to check the outlet/ check whether the textView is added to the ViewController properly. I think this should solve your problem.
There is an issue with your text view setup.
If an outlet is connected to the text view, you do not need to create it in code (which you are doing).
If you create it in code, you have to add it as a subview to the desired view (which you are not doing).
I think you forgot to add the textview in your parentview; I assume you are creating runtime textview. Just add this line in your code.
[self.view addSubview:textView];
I have this really unanswered question.. after looking in the web for this problem i tring my luck with you..
i have a UITextView that gets the string from url.
but... the textfield is cuts/hidden fron the half first row and its fixed only when i touch it.
this is the code:
- (NSString *)getMonthValues{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#",URL_ADDRESS2,URL_MONTH_ZODIAC]];
NSString *Value = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
Value = [Value stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
}
-(void)getMonthValue{
NSString *horoscope_daily = [self getMonthValues];
[Month_horoscope setText:[NSString stringWithFormat:#"%#",horoscope_daily]];
}
can someone please explain this problem and help me fix it ?!
It is either 1) covered by other view, or 2) has a frame too small to see the whole text.
To check out the cover issue, try setting background property to [UIColor redColor], on all other visible views.
And if it is the frame issue, right after you setText, issue:
[Month_horoscope sizeToFit];
I am making a multi- timer app that uses plist files. the plist files are loaded into a tableview. I don't want the .plist extension from showing in the tableview. Is there a solution for this?
Here is the code that I need help with. this is a NSArray not NSString
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
NSString * filenameWithExtension = #"timer1.plist";
NSString * filename = [filenameWithExtension stringByDeletingPathExtension];
returns a string without extension.
You can set the string as table content.
cell.textLabel.text = filename;
The tableview only represents the data, in this case the file name with is a NSString. Before you set the cells contents just rename the string
NSString *filenameWithExtension = #"timer1.plist";
NSString *filename = [filenameWithExtension stringByReplacingOccuranceOfString:#".plist" withString:#""];
cell.textLabel.text = filename;
There are other String/Path helpers that can help you achieve the desired effect too.
I have a UITextView in a DetailViewController with some text which is loaded from a NSMutableDictionary in the MainViewController. Here is the code for that;
- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"This is some text", #"textkey", nil];
detailViewController.myDictionary = myDictionary;
}
I have loaded the string from this into my DetailViewController with this code;
self.myText.text = [(NSMutableDictionary *)myDictionary objectForKey:#"textkey"];
Also in my viewDidLoad I have created a RightBarButton named 'Save' which I use to hide the keyboard when the viewer is done editing. I would like this button to also save the changes the viewer enters into the UITextView (as well as the original text).
This is the code for the rightBarButton;
UIBarButtonItem *saveButton =[[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(textViewDidChange:)];
self.navigationItem.rightBarButtonItem = saveButton;
Lastly I have code which envokes the textVidewDidChange and hides the keyboard. I am trying to also have it save the changes to the textView but it doesn't.
-(void)textViewDidChange:(UITextView *)textView;
{
if( textView == myText )
[myDictionary setValue:myText.text forKey:#"textkey"];
[myText resignFirstResponder];
}
Can anyone help me accomplish this. I simply want to save the changes to the UITextView back to the NSMutableDictionary. (or maybe not so simply)
I have changed the button to
`UIBarButtonItem *saveButton =[[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(didChangeValueForKey:)];
self.navigationItem.rightBarButtonItem = saveButton;`
and other code to ;
`-(void)didChangeValueForKey:(NSString *)key{
NSError *error;
[myDictionary setValue:[self myText].text forKey:#"textkey"];
[myText resignFirstResponder];
NSLog(#"%#", [myDictionary valueForKey:#"textkey"]);
}
My NSLog shows the changes but when I reopen the app they are gone, not saved. Can one save directly to the NSMutableDictionary?
I read a lot on persistent data. Thought NSData or Plist but try as I may not doing well.
Can someone suggest a good tutorial on this?
I looked at suggested link and added this (bold part) to my code.
`-(void)didChangeValueForKey:(NSString *)key{
/* NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:#"textkey"]];*/
NSError *error;
[myDictionary setValue:[self myText].text forKey:#"textkey"];
**[myDictionary writeToFile:#"textkey" atomically:YES];**
[myText resignFirstResponder];
NSLog(#"%#", [myDictionary valueForKey:#"textkey"]);
}`
As you can see I also tried getting the path using [NSHomeDirector] above and then replaced
#"textkey" with path. I can still see the changes in NSLog (either way) but there is no change when I reload the view or relaunch the app.
I have change things so that I am saving a text file of the text in the textview with the name gotten from the dictionary so that each time a different detailView is loaded depending on the image selected in the mainViewController.
This is my dictionary entry in the mainViewController;
`if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//here is where I want to access the text file name to load in the UITextView in my DetailView
#"first View Text", #"textkey2",
//This is where I give the text file of the changed text its name for each different view of the DetailView
#"first View Text", #"textkey3",nil];
detailViewController.myDictionary = myDictionary;
}`
Next is the code I use to save the changes to the textView using the UIbarbuttonright
`- (void)saveAction:(id)sender {
[self.myText2 resignFirstResponder];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:#"textkey3"]];
NSString *savedString = myText2.text;
NSLog(#"The Save file is:%#", savedString);
[savedString writeToFile:documentTXTPath atomically:YES
encoding:NSUTF8StringEncoding error:NULL];
}`
I have checked this and the file is saved in the documents folder under the name of First View Text and it does contain the changed text.
I am having problems loading the text file contents into the UITextView.
Using the code I have I get the path to the textkey2 object (First Text View) not the contents of the file.
`NSString *textName = [myDictionary objectForKey:#"textkey2"];
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *fullPath2 = [documentsDirectory2 stringByAppendingPathComponent:textName];
/* self.myText2.text = fullPath2;*/
self.myText2.text = [NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL];`
I replaced this last line with the one that isn't commented out and it works fine. For anyone who want to know.
You need to implement data persistence, e.g. saving the text into permanent storage that you can retrieve again when you reload the view later on.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ConvoreCell";
ConvoreCell * cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ConvoreCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ConvoreCell *) currentObject;
break;
}
}
}
NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];
NSString *picURL = [[cellValue objectForKey:#"creator"] objectForKey:#"img"];
if ([picURL isEqualToString:#"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
picURL = #"https://convore.com/media/images/eric.png";
if ((picURL != (NSString *) [NSNull null]) && (picURL.length !=0)) {
NSLog(#"%#", picURL);
NSData *imgData = [[[NSData dataWithContentsOfURL:
[NSURL URLWithString:
picURL]] autorelease] retain];
[cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];
} else {
cell.pic = nil;
}
//NSLog(#"Name is : %#", [cellValue objectForKey:#"name"]);
cell.title.text = [cellValue objectForKey:#"name"];
cell.info.text = (#"Created by: %#", [[cellValue objectForKey:#"creator"] objectForKey:#"name"]);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
For some reason the image is not showing up. What am I doing wrong here? The URL of the image is valid, I checked.
Also I don't want to have the accessory view on the TableViewCell, I did specify that as no in IB, but it still shows up... i set the accessory attribute to none already
you have some issues in your code.
NSData *imgData = [[[NSData dataWithContentsOfURL:
[NSURL URLWithString:
picURL]] autorelease] retain];
the former is not a real issue (maybe by coincidence?!), but it's ugly and confusing. get rid of the autorelease and the retain. The object you get from dataWithContentsOfURL: is already autoreleased.
[cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];
And your real issue might be this. From what I know you should call init exactly one time; after you've allocated an object.
So if cell.pic is an UIImageView I would try to change the code to something like this:
cell.pic.image = [[[UIImage alloc] initWithData:imgData] autorelease];
Once you see your images you should change your code so that it saves the images in the data you use to populate the cell. You are downloading the image every time a cell appears. That's something you shouldn't do. But this is beyond this question.
EDIT:
Also I don't want to have the
accessory view on the TableViewCell, I
did specify that as no in IB, but it
still shows up... i set the accessory
attribute to none already
You do? But I can see this line of code:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
You should be able to get an UIImage by just
[cell.pic initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSUrl NSURL picURL]]]];
You're not showing your cellForRowAtIndexPath method, which is the key to analyzing what's going on. The typical paradigm is that UITableViewCell objects get reused repeatedly by the UITableView. You should be setting the UIImage into the cell when the UITableView calls cellForRowAtIndexPath.
The problem causing your immediate crash is that the nib for ConvoreCell is trying to set an IBOutlet you have declared but finding no way to actually set it. I would check that you have synthesized your pic property.
There are other problems here. The line
[cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];
Is wrong; you do not call init on an object that already exists, but only when you are allocating an object. Do something like
cell.pic = [[[WhateverClassPicIS alloc] initWithImage:[[UIImage alloc] initWithData:imgData]];
Also, you are using NSData dataWithContentsOfURL on the main thread. This will lock up your user interface for an indeterminate time as you connect to the network. Use asynchronous calls instead.