NSMutablearray addObject is not working it overwrites data - iphone

I'm a beginner to XCode.
1- I have a view with UITableViewCell(FirstViewController)
2- I have another view(SecondViewController) that contains some textfields to get data from user
3- I have to pass this data to the FirstViewController
4- The data is passed successfully to the FirstViewController and successfully added in the TableViewCell
5- but the problem is that....
6- if I add another data, it is added but the previous cell get blanked.
How can I preserve the previous data while saving new one????????
7- temp is variable where data is collected...
8-here (item) is NSMutablearray.. It adds object(temp) to item using
[self.item addObject:temp ];
.. but old value of temp get lost ;(
///my Code
data *temp;
temp =[[data alloc]init];
temp.name=NSLocalizedString(textofsub, #"name");
temp.detail=NSLocalizedString(#"Teacher", #"detail");
temp.image=#"a.jpg";
temp.time=NSLocalizedString(#"8:30", #"time");
[self.item addObject:temp ]; //
[self.mytableView reloadData];

you have to initialize array on viewDidLoad
self.item =[[NSMutablearray alloc]init];

Related

NSArray to NSMutableArray

I am trying to pass an NSArray to a NSMutableArray but am having a few issues doing so and was hoping someone could help.
- (void)CachedData:(NSArray *)gArray indexPath:(NSIndexPath *)gIndex dataSetToParse:(NSString *)string
{
//Set dataSetToParse so the parsing delegates if statment works correctly
dataSetToParse = string;
//Calls method that sets the accessory tick
[self setAccessoryIndexPath:gIndex];
//Set array that can be used in this view
[parsedDataArray addObjectsFromArray:gArray];
//Use this method to pass NSData object to startTheParsingProcess method and also to initalize indexPathVariable
[self startSortingTheArray:gArray];
}
When I try to log parsedDataArray after I addObjects to it all I get is
(null)
(null)
.... etc.
any help would be appreciated.
Update
The main issue here is that I have 4 lots of arrays. with 3 views, a main view sub view and then a sub subview.
each array has a refrence ID to the next array, I want to parse each array before I display them to check if there are any values in them based off the restriction string. If there are no values then I will either send the user back to the main view or just not allow them to select the cell that has an array with no related values. (I hope you get what I am doing here)
The solution I have come up with for that is to parsed the values in the parent view of where I intend to display them...
if you look at my views
view 1
- view 2
-- view 3
I need a parsing delegate for view 1 and 2, because I need to check the values of view 3 in view 2.. However this is causing me an issue because I am using the same array to avid redundancy to return the value to the main view and create/check the values of the subview.
So I am trying to skip over the parser delegates of view 2 if i am not going to display anything in view 3, by passing my already parsed array into the mutablearray I will pass the data back with inside didselectcell method...
Anyway I hope this makes sense.. its the only way I think I can do this.. if you know better please let me know your strategy.
Assuming the parsedDataArray is a mutable array declared somewhere else, this wouldn't make sense:
//Set array that can be used in this view
[parsedDataArray addObjectsFromArray:parsedDataArray];
Unless you mean to double all of the objects in the parsedDataArray. I think what you meant is:
//Set array that can be used in this view
[parsedDataArray addObjectsFromArray:gArray];
But I can't be sure without more context or explanation.
check if garray is containing the data or not, by printing it in nslog
also check if u have intialized parsedDataArray as below or not
parsedDataArray=[[NSMutableArray alloc]init]; in Viewdidload delegate
NSArray *array = [NSArray arrayWithObjects:#"Raja",#"Ram",nil];
NSMutableArray *muArray = [NSMutableArray arrayWithArray:array];

creating a Mutable array that can be added to in later clicks of the same button?

General noob questions:
(1) How can I create an NSMutable array in a buttonClicked action that I can add more entries to during subsequent clicks of the same button? I always seem to start over with a new array at every click (the array prints with only 1 entry which is the most recent button's tag in an NSLog statement).
I have about 100 buttons (one for each character in my string called "list") generated by a for-loop earlier in my code, and each has been assigned a tag. They are in a scrollview within the view of my ViewController.
I wish to keep track of how many (and which ones) of the buttons have been clicked with the option of removing those entries if they are clicked a second time.
This is what I have so far:
-(void) buttonClicked:(UIButton *)sender
NSMutableArray * theseButtonsHaveBeenClicked = [[NSMutableArray alloc] initWithCapacity: list.length];
NSNumber *sendNum = [NSNumber numberWithInt:sender.tag];
[theseButtonsHaveBeenClicked addObject:sendNum at index:sender.tag];
NSLog(#"%#",theseButtonsHaveBeenClicked);
}
(2) I have read that I may be able to use a plist dictionary but I don't really understand how I would accomplish that in code since I cant type out the items in the dictionary manually (since I don't know which buttons the user will click). Would this be easier if I somehow loaded and replaced the dictionary in a plist file? And how would I do that?
(3) I also have no idea how I should memory manage this since I need to keep updating the array. autorelease?
Thanks for any help you can provide!
Okay, firstly you are creating a locally scoped array that is being re-initialised on every call to buttonClicked:. The variable should be part of the class init cycle.
You will also be better off with an NSMutableDictionary instead of an NSMutableArray. With a dictionary we don't have to specify capacity and we can use the button's tags as dictionary keys.
Here's what you need to do, these three steps always go together: property/synthesize/release. A good one to remember.
//Add property declaration to .h file
#property (nonatomic, retain) NSMutableDictionary * theseButtonsHaveBeenClicked;
//Add the synthesize directive to the top of .m file
#synthesize theseButtonsHaveBeenClicked;
// Add release call to the dealloc method at the bottom of .m file
- (void) dealloc {
self.theseButtonsHaveBeenClicked = nil; // syntactically equiv to [theseButtonsHaveBeenClicked release] but also nulls the pointer
[super dealloc];
}
Next we create a storage object when the class instance is initialised. Add this to your class's init or viewDidLoad method.
self.theseButtonsHaveBeenClicked = [[NSMutableDictionary alloc] dictionary]; // convenience method for creating a dictionary
And your updated buttonClicked: method should look more like this.
-(void) buttonClicked:(UIButton *)sender {
NSNumber *senderTagAsNum = [NSNumber numberWithInt:sender.tag];
NSString *senderTagAsString = [[NSString alloc] initWithFormat:#"%#",senderTagAsNum];
// this block adds to dict on first click, removes if already in dict
if(![self.theseButtonsHaveBeenClicked objectForKey:senderTagAsString]) {
[self.theseButtonsHaveBeenClicked setValue:senderTagAsNum forKey:senderTagAsString];
} else {
[self.theseButtonsHaveBeenClicked removeObjectForKey:senderTagAsString]; }
[senderTagAsString release];
NSLog(#"%#", self.theseButtonsHaveBeenClicked);
}

Array concatenation

I have a table view with 3 rows, if I select the first row , the item on the first row get saved in a array variable say arrdata , similarly when I select 2nd and 3rd row , I want it to get saved in the same variable arrData.
You can use NSMutableArray.
For Ex.
NSMutableArray *arrData = [[NSMutableArray alloc] init];
Now, select the first row from table view
Use [arrData addObject:[first row data]];
For 2nd from table view
Use [arrData addObject:[2nd row data]];
For 3rd from table view
Use [arrData addObject:[3rd row data]];
And you can also add some condition for add data in to arrData, like if 1st row data is added then do't re-add it.
For Ex. When you select 1st row you can add 1, 2nd selection add 2, 3rd selection add 3 like [arrData addObject:#"1"]; etc.
Now, If you want to get data from arrData,
If (arrData.count > 0)
{
NSString *fData = [arrData objectAtIndex:0]; // Here you get stored data from 1st selected row of table.
}
Create an nsmutable array and add objects by using method addobject
NSMutableArray *arrData =[[NSMutableArray alloc]ini];
On Tableselection method , add the following line
[arrdata addObject:your object];

getting user specific data from a webservice to populate a UITableView

I am trying to populate a UITableView with data for a specific user (his projects) from a webservice, after the user as logged into the app.
for example:
tableData = [[NSMutableDictionary alloc] initWithCapacity:DICTIONARY_INITIAL_CAPACITY];
//CURRENT PROJECT
//get the information from the server
NSArray *currentProject = [[IHMObjectFinderServices sharedIHMObjectFinderServices] getCurrentProjectAsArray:connectedUserId];
if(![currentProject isEqual:[NSNull null]]) {
//put the info in a dictionnary
NSMutableDictionary *currentProjectDictionary = [[NSMutableDictionary alloc] initWithCapacity:PROJECT_DICTIONARY_CAPACITY];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_NAME] forKey:KEY_PROJECT_NAME];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_MANAGER] forKey:KEY_PROJECT_MANAGER];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_TAG] forKey:KEY_PROJECT_TAG];
NSArray *activitiesForProject = [NSArray arrayWithArray:[[IHMObjectFinderServices sharedIHMObjectFinderServices] getAvailableActivities:[currentProjectDictionary valueForKey:KEY_PROJECT_TAG]]];
[currentProjectDictionary setObject:activitiesForProject forKey:KEY_PROJECT_ACTIVITIES];
[tableData setObject:currentProjectDictionary forKey:KEY_CURRENT_PROJECT];
At the moment, my table loads in viewDidLoad method, so the problem is that the user is not yet connected when the table is constructed.
I have read about the reloadData method but I am not sure how to proceed to load the table only once, once the user has logged in. Could someone explain to me what is the correct procedure for this ?
Thanks for your help,
Michael
There should be some kind of call back method when the data for the tableView is available. Just put your reloadData in it.
- (void)dataIsAvailable
{
// ... your code to set the tableData ...
[self.tableView reloadData];
}

How to pass values between two Custom UITableViewCells in iphone

I have two customized cells in an iphone UITableViewController and would like to capture the data in one cell into another.
I am currently unable to post images and please assume the following points.
There are two custom UITableViewcells
Notes Textfield in one customcell
Submit button in another text field.
Now I want to pass data typed in the notes textfield to be available when I click the Submit button. Is this possible? Please help :(
In general you should store your data separate from views. So if you have some table controller there must data objects like array or dictionary or array of dictionaries. And when you click submit you should get associated data object and work with it.
In your case when textfield lose focus you must get textfield value and store it in your data store.
UPD: It not very clean but can help at first
- (void) textFieldDidEndEditing: (UITextField *) textField {
// At first need to get cell. findSuperviewWithClass is custom method to find proper superview object
MyCellClass *cell = (MyCellClass*)[textField findSuperviewWithClass:[MyCellClass class]];
// and indexPath for it
NSIndexPath *index = [tableView indexPathForCell:cell];
// tableData is NSArray of my objects
MyDataObjClass *dataObj = [tableData objectAtIndex:index.row];
dataObj.text = textField.text;
}