I need to create a NSString for browsing folders in FTP share. i show the directory on a TableView, and user´s can browse by selecting row ´s
Im writing the string of selected rows into a mutable array, and then i need to make a string of all strings in the mutable array. means add the last string to the previous when the row is selected
for example first string in array is "Downloads" second "Movies" third "HD-Movies"....... and so on
for that i need the string on the first time selected row "/Downloads/"
the second time "/Downloads/Movies/", an the third "/Downloads/Movies/HD-Movies"
i´m sure i need a NSMutableString, but don´t know how to add the strings...
here a part of my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *filePathArray = [[NSMutableArray alloc]init];
[filePathArray addObject:#"/Downloads/"];
[filePathArray addObject:[fileNameArray objectAtIndex:indexPath.row]];
}
You can do like this:
NSString *string=[array componentsJoinedByString:#"/"];
This will give you :
Downloads/Movies/HD-Movies
Now if you want / in front than you can simply append an /.
Related
I have an interesting requirement for an app I am working on. I have a UITableView with a bunch of items in it. I am trying to log which items were looked at (scrolled past). For example, if the list contains letters A-Z and the user scrolls down the UITableView to letter T, then values A,B,C,D,E.F....T would be stored as an NSString in NSUserDefaults.
I would like to store the actual row text as the value in the NSString. For example, A instead of 1, B instead of 2, etc...
I have done some digging around but cant seem to find anything useful. Any ideas on how I might accomplish this?
The function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
is called every time you view a cell. You could then use a method to convert indexPath.row into an alphabetic letter and store that in NSUserDefaults
Edit - on re-reading your question I get the impression youre just talking about generic titles of the rows. Thats easy - because you have to set the title text in cellForRowAtIndexPath - so you could just pop it into defaults when you do this.
Make a plist ( NSDictionary ) and make pairs ( key- value ) like 1 - A 2- B and so on. When you want to store it, write a method, which will search your value, for a key.
if you have dynamic data:
make an NSMutableDictionary, and
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[dict setObject:cell.label.text forKey:indexPath.row];
}
EDIT:
You can get the visible rows with this method:
indexPathsForVisibleRows
Returns an array of index paths each identifying a visible row in the receiver.
- (NSArray *)indexPathsForVisibleRows
Return Value
An array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. Returns nil if no rows are visible.
I assume you are populating the list from array containg A,B,C and etc.
Do your array would be:
(A,B,C,D,E,F,G)
So, start with converting each index of array into a NSMutableDictionary such that the array becomes as follows, here visited refers to wether that row has been visited or no, by default 0 for No:
(
{
value = "A"
visited = 0
},
{
value = "B"
visited = 0
},
{
value = "C"
visited = 0
},
{
value = "D"
visited = 0
},
)
Now on cellForRowAtIndexPath do this:
NSMutableDictionary *editDict = [arrayObjects objectAtIndex:indexPath.row];
[editDict setInteger:1 forKey:#"visited"];
[arrayObjects replaceObjectAtIndex:indexPath.row withObject:editDict];
This way all the rows that have been visited will have 1 for key "visited"
then you can run a loop to save the values corresponding to visited = 1 wherever you want
I parsed and stored an XML in two mutable arrays,they are albumArray and trackArray. I created an dictionary using these two arrays and that is as follows,
trackANDAlbum = [NSMutableDictionary dictionaryWithObjects:trackArray forKeys:albumArray];
so my dictionary looks like this :
album1 = song1
album1 = song2
album1 = song3 etc.
Since the albumArray contains duplicates, I eliminated it using NSSet and this new array called "eliminateDupe" is given as the data source for a UITableView.
The problem I face is that, when the user selects a particular album name in the TableView then, the corresponding songs of the selected album must be displayed in another view.
So is it possible to identify what album name is selected in DidSelectRow of TableView and provide that as a key for the dictionary trackANDAlbum, and retrieve the corresponding songs and display it in an tableview.
For eg, if the selected album is "album1" so in the next tableview songs corresponding to album1 must be listed,that is song1,song2,song3.Any possibilities to achieve this concept, or else do I have some better ways?
If you are using albumArray to populate your tableview than using this code you can get album name on touch of row in tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * albumName = [albumArray objectAtIndex:indexPath.row];
}
And you can use this albumName variable as key to find song name from your dictionary, if you are using another array then change array name according,and yes if you want to use albumName variable outside of this method than make its property in .h file.
UPDATE
For retrieving data from dictionary using our albumName variable
if([yourDictionary objectForKey:albumName]!=nil)
{
NSString * songName = [yourDictionary objectForKey:albumName];
}
You can retrieve data using this code, and yes as I said to use albumName in another method you have to make its property in .h file and synthesize it in .m file.
EDIT
In your case you have multiple song name for single album name so your code going to be like this.
if([yourDictionary objectForKey: albumName]!=nil)
{
NSArray * newSongAry;
[newSongAry addObject:[yourDictionary objectForKey:albumName]];
}
I currently have a function written called saveWorkout that saves an NSMutableArray to another NSMutableArray from a Singleton class. This function works the first run through, however, when I run it a second time, it erases what was previously stored in element 0 and replaces it with the new array (which is a collection of strings gathered when a user clicks on a table).
Here is my function:
-(IBAction)saveWorkout{
WorkoutManager *workoutManager = [WorkoutManager sharedInstance];
[[workoutManager workouts] insertObject: customWorkout atIndex: 0];
NSLog(#"%#", [workoutManager workouts]);
}
customWorkout is what initialially creates the NSMutableArray (based on what the user clicks). Thus, if my first array is comprised of blah1, blah2, those two values will be stored in the workouts array. However, if I then click blah2, blah 3, the workouts array will have two identicle arrays (blah2, blah3) and it doesn't retain the first array. Any idea why this is happening?
Here is how I form customWorkout:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[customWorkout insertObject:str atIndex:0];
//Test Code: Prints Array
NSLog(#"%#", customWorkout);
}
I will tell you the logical mistake that you are making....
you are using the same customWorkout object over and over again to insert in the workouts array... (so its the same pointer) whereas what you need to do is to create a copy of the customWorkout array and then insert it in the workout array ... try this instead....
[[workoutManager workouts] insertObject: [[customWorkout mutableCopy] autorelease]atIndex: 0];
this should work unless you are doing something else in your code.
[[workoutManager workouts] insertObject: customWorkout atIndex: 0]; does not copy the contents of customWorkout ... instead it just retains a reference to customWorkout. So your code is simply storing multiple references to the same object, which you end up (unintentionally) editing on the second run through.
You need to either:
Copy the customWorkout object via copy when you store it in workouts
OR:
Assign customWorkout to a new NSMutableArray instance each time after you do a saveWorkout
Either route should keep you from modifying the NSMutableArray you store into the workouts collection. The first option is probably more clear in terms of memory-management...
I have a UITableView and i can add and delete cells. I also have two buttons on each cell to add and subtract 1 from the cells text. But when i go to a different page and then back to the table view page, all the cells text is set back to 1. But i want the cell's text to stay at the value that the user had set it to! Could someone help me? Im not sure what to do whatsoever. Thanks!
You will have to maintain an NSMutableArray (probably of NSIntegers) that saves the values of the cells. Then, whenever the user changes the value, update the value in the array. Also, display the cell label values by reading from the array. Sample codes below-
-(void)plusButtonClicked:(id)sender
{
//figure out the cell index which was updated using sender
//update the array entry
//[self.array objectAtIndex:index]++; self.array is the array you will maintain
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.label.text = [NSString stringWithFormat: #"%d", [self.array objectAtIndex:indexPath.row]];
}
If you want the values to persist even after the app is terminated and restarted, consider using CoreData.
I have a UITableView that, under certain conditions, needs to have something added to the top of it. All data (except for what is inserted at the top of the UITableView under certain conditions) is brought in from an array.
Because everything is brought in from an array, I need to modify the indexPath that fetches those array objects each and every time the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called. If I try to create a local variable and update the local version of indexPath.row, it tells me it is read only.
What would be the best way to implement this?
Drawing below (this is not intended to be code, but a drawing of the table view):
(REGULAR SITUATION) (3 lines)
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;
etc. etc
(MODIFIED SITUATION) (4 lines)
blah blah modified insertion text here
-----
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;
etc etc
Thanks in advance.
Why not just add your new item at the start of your array?
// Create a mutable copy and add the item at index 0
NSMutableArray *mutable = [myData mutableCopy];
[mutable insertObject:newItem atIndex:0];
// Then store the new array and reload the table
[myData autorelease];
myData = mutable;
[self.tableView reloadData];
Then you don't have to do anything funny at all with index paths :)
THIS is a great tutorial that addresses your issue.
Use
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
OR
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Return the String/View as something or empty depending on the condition that you use while deciding when to show it or not.
Don't go down that path. Just use the tableHeaderView property of UITableView to add something on top of the table. It will scroll just like a UITableViewCell.
self.tableView.tableHeaderView = aView;
To remove it, just set it to nil.
If you insist in this method, just keep a BOOL around to tell you in which state you are and if you need the extra line just subtract 1 from indexPath.row, like
[myArray objectAtIndex:indexPath.row-1];