Issue in adding dynamic cell in table view? - iphone

Please have a look on my code which i am using to add dynamic cells in my table view add run time. At did select of my table view i have called this method.
- (void) allServersFound
{
// called from delegate when bonjourservices has listings of machines:
NSArray *newPosts = [[NSArray alloc]initWithObjects:#"A", nil]; // NSArray of machine names;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: #"A",#"B",#"C",#"D", nil];
int i = 0;
for (NSArray *count in newPosts)
{
[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
}
[[self tblHome] beginUpdates];
[[self tblHome] insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationNone];
[[self tblHome] endUpdates];
[tempArray release];
}
But this give me following Exception at run time:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSIndexPath _fastCStringContents:]: unrecognized selector sent to instance 0x4e0e130

First you initialize your tempArray with strings like this:
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: #"A",#"B",#"C",#"D", nil];
Then you add indexPaths like this:
[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
so the array you pass to the insertRowsAtIndexPaths method contains both strings and indexPaths objects. I think this is the reason of the exception.

As others noted, your tempArray contains a mixture of NSString and NSIndexPath objects. This is the most obvious thing that you need to address before you do anything else. You can use [NSMutableArray array] class method for that (it's autoreleased, so you'll need to remove the call to [tempArray release] at the end of your method).
A less obvious thing is that the model of your UITableView must be updated before you call insertRowsAtIndexPaths, otherwise you would get another exception in a much less obvious place.

Your tmparray contains NSString values as well as the IndexPaths, try removing the NSStrings first.
Try replacing the line
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: #"A",#"B",#"C",#"D", nil];
with the line
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:nil];

Related

Synthetise an NSMutableArray

for(int i=0;i<[promotionArr count];i++)
{
// NSLog(#"ok");
Promotion *prom = [[Promotion alloc]init];
prom.pName=[[promotionArr objectAtIndex:i] objectForKey:#"name"];
prom.pUrl=[[promotionArr objectAtIndex:i] objectForKey:#"url"];
prom.pDescription=[[promotionArr objectAtIndex:i] objectForKey:#"description"];
[promMUAraay addObject:prom];
NSLog(#"number of elements : %d",[promMUAraay count]);
}
But the number of element is always 0 . I haven't do #synthetise for the NSMutableArray , xcode tell me that i can't .I just do this in my .h
NSMutableArray *promMUAraay;
It's that the problem ?
Have you remembered to alloc, init the array?
NSMutableArray *array = [[NSMutableArray alloc] init];
Do this before you use the array.
You should initialize your NSArray before accessing it. That is, in your .m file, in your -init method, you should have some code like this:
promMUAraay = [[NSMutableArray alloc] init.......];
For all the options you have initializing an NSArray, see the NSArray reference.
As to your attempt at syntethizing the array, the #synthetize keyword is used to automatically create definitions for setter/getter methods to your array (provided you have a corresponding #property declaration in your interface). This does not change things in that you would all the same need to initialize your NSArray in the init method of your class. The only thing is that you could thereafter refer your variable using the dot-notation: self.promMUAraay.

Problem with arrays (add and copy)

I've got a problem. I'm trying to sort the items from 1 array (name: mps) into an other array (name: totalArray), but every item should be in a new array. In short: the items in mps should move into a new array, but every item should be in a new array, so the totalArray is an array of arrays. After that, totalArray should be copied back into mps.
To clarify, here is the code:
NSMutableString *oud = [[NSMutableString alloc] init];
NSMutableString *nieuw = [[NSMutableString alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray removeAllObjects];
NSMutableArray *totalArray = [[NSMutableArray alloc] init];
[totalArray removeAllObjects];
tempArray = [[NSMutableArray alloc] init];
[tempArray removeAllObjects];
for (int j = 0; j < [mps count]; j++)
{
nieuw = [[mps objectAtIndex:j] valueForKey:#"ConfigurationAlias"];
if (j != 0)
{
if ([oud rangeOfString:nieuw].location == NSNotFound)
{
NSLog(#"ADDED!");
[totalArray addObjectsFromArray:tempArray];
[oud setString:nieuw];
[tempArray removeAllObjects];
[tempArray addObject:[mps objectAtIndex:j]];
}
else
{
[tempArray addObject:[mps objectAtIndex:j]];
}
}
else {
[oud setString:nieuw];
[tempArray addObject:[mps objectAtIndex:j]];
}
}
//En de array weer laden in de tabel
//NSLog(#"%#", totalArray);
NSLog(#"%i", [mps count]);
[mps removeAllObjects];
NSLog(#"REMOVED!!!");
NSLog(#"%i", [mps count]);
[mps addObjectsFromArray:totalArray];
NSLog(#"Added totalArray: %i", [totalArray count]);
NSLog(#"%i", [mps count]);
[tempArray release];
[totalArray release];
[tabelView reloadData];
}
Now, here comes my problem:
The log gives the line 2011-04-07 10:02:46.368 Fleet Manager[901:40b] ADDED! 11 times, then it posts 16 (mps count, this is correct), then the line REMOVED!, then 0 (mps count, also correct), but then the line: Added totalArray: 15. This can't be correct, it added 11 times but it counts 15?
This a problem for me, in the first place because it's plain incorrect, but the tableview gets screwed up as well because of the section-amount not being correct either (15 sections). Anybody has any idea what's going on?
EDIT: all the objects in mps are dictionaries, and all of them have a value for the key "ConfigurationAlias".
First, I would like to suggest using debugging mode instead of bunch of NSLog functions to track down what's actually happening inside your objects and variables. Try using breakpoints. Set one for [totalArray addObjectsFromArray:tempArray]; line so every time this line of code is reached the execution would stop and let you look around, check different fields' states inside totalArray and tempArray objects.
Concerning your problem... Since you are using addObjectsFromArray: that means that passed array may pass not one but several objects (thus the plural in method name). I guess one of those "11 times" added more than one element - that should explain the actual count being more than the times you've called this method. Again, please check tempArray contents.
Good luck!
If you want to end up with an array of arrays, you need to be allocating temp arrays inside the loop. It also seems like you are overdoing it with clearing out the arrays.
It appears you are trying to add the value from mps to totalArray regardless of the conditions, so it would make sense to only do that in one place (outside of the conditions).
Also, if you are not mutating the arrays (such as tempArray), you can use NSArray instead, which should be smaller and faster.
Finally, you should be able to assign the new array to mps instead of doing the expensive array copying. Not sure if that is somehow referenced as the table delegate, though.
I would try something like:
NSMutableString *oud = [[NSMutableString alloc] init];
NSMutableString *nieuw = [[NSMutableString alloc] init];
NSMutableArray *tempArray;
NSMutableArray *totalArray = [[NSMutableArray alloc] init];
for (int j = 0; j < [mps count]; j++)
{
tempArray = [[NSMutableArray alloc] init];
[totalArray addObject:tempArray];
[tempArray release];
nieuw = [[mps objectAtIndex:j] valueForKey:#"ConfigurationAlias"];
if (j != 0)
{
if ([oud rangeOfString:nieuw].location == NSNotFound)
{
NSLog(#"ADDED!");
[oud setString:nieuw];
}
}
else {
[oud setString:nieuw];
}
}
NSLog(#"%i", [mps count]);
[mps removeAllObjects];
NSLog(#"REMOVED!!!");
NSLog(#"%i", [mps count]);
[mps addObjectsFromArray:totalArray];
NSLog(#"Added totalArray: %i", [totalArray count]);
NSLog(#"%i", [mps count]);
[totalArray release];
[tabelView reloadData];
}
You should
sort the base array in a tempArray
create an empty resultArray
iterate through tempArray and for each item create an itemArray containing the item on the first position and add this array to resutArray
don't forget to release all alloc-ed temporary data
Check this out for sorting easily: Sorting arrays via Apple

Passing NSMutableArray from delegate

I'm doing an Iphone aplication and in the delegate class i call a method from another class which return a NSMutableArray filled with the information i need:
NSMutableArray *array = [[NSMutableArray initWithObjects:nil] retain];
array = [xml loadXML:#"info.xml"];
Now I want to pass this array into the viewController class so i can do things with my mutable array. I do the following:
...
[self.window addSubview:viewController.view];
[self.viewController loadLocations:array];
[self.window makeKeyAndVisible];
In delegate, the array is ok, it has the data i want, however, in the viewController class (which is a UIViewController) the array is messed up.
-(void)loadLocations:(NSMutableArray*)_array{
NSLog(#"%f", [[_array objectAtIndex:0] lat]); // This sould be 42.000 but it is 0.00000 and all of the other indexes
You're in trouble right from the beginning:
NSMutableArray *array = [[array initWithObjects:nil] retain];
You're calling "initWithObjects" on "array", but you haven't allocated "array" yet.
You want something like:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
or just :
NSMutableArray *array = [[NSMutableArray alloc] init];
This part is invalid:
NSMutableArray *array = [[array initWithObjects:nil] retain];
array = [xml loadXML:#"info.xml"];
The first line is not used because the second line is setting the array pointer to the result of [xml loadXML:]
I think this should suffice:
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[xml loadXML:#"info.xml"]];

-[NSCFString objectAtIndex:]: unrecognized selector

I have this little problem I couldn't find in Google:
UITableView works fine until I start scrolling.
Error Message: `-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5f24870 `
Method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
//populate cell
cell.textLabel.text = [[myArray objectAtIndex: storyIndex] objectForKey: #"subject"];
return cell;
}
Just guessed that was the "problem"-method... if you need more infos: please tell me :)
thanks for all your help!
EDIT:
myArray is just the normal NSArray *myArray
It's a sorted Array ->
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"lesson" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
myArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];
resultArray (NSMutableArray) is the Result of an XML-Parser.. hope you can see my problem..
It means that you're sending an objectAtIndex: messages to an object of type NSString (or possibly CFString). Of course you're expecting myArray to be an array and not a string.
What probably happened is that your array is being released (almost certainly autoreleased) too early and that by the time the method above is called the memory that was being pointed at by myArray now holds a string.
In response to your edit...
This line:
myArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];
...is incorrect. The value is autoreleased. You need to retain it:
myArray = [[resultArray sortedArrayUsingDescriptors:sortDescriptors] retain];
Remember to release it before you close the view!
myArray is proberbly autoreleased. Use self.myArray to set the sorted array. (with the correct #property (retain) )
If you have correctly identified this method as the culprit I'm guessing this line is the problem:
cell.textLabel.text = [[myArray objectAtIndex: storyIndex] objectForKey: #"subject"];
To me it looks lime myArray is an instance of NSCFString and not a NSArray. You should start by double checking that and that you never assign anything but NSArray instances to myArray.
--edit--
Try
self.myArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];
That should correctly retain the array for yoy to make sure it is not garbage collected and replaced with a rouge string :)
If myArray is an instance variable instead of a property, try:
myArray = [[resultArray sortedArrayUsingDescriptors:sortDescriptors] retain];

Table crashes when sorting the data multiple times

I have a tableview with a navigationBar with a segmentedControl on the top of the view. I have set up the segmentedControl with buttons that sort the table by either "FirstName" or "LastName". It works perfectly the first 2-4 of times you press the sorting buttons, but then the app crashes.
The debugger and console seem to be of no help finding the source of the bug. Does anyone see any glaring mistakes in my code?
Here is my code below, let me know if you have any questions. Thanks!
- (IBAction)sortingSegmentAction:(id)sender{
NSString *keyToSortBy = [NSString alloc];
if([sender selectedSegmentIndex] == 0)
{
self.sortingSegmentActionPressed = 0;
keyToSortBy = #"FirstName";
}
else if([sender selectedSegmentIndex] == 1)
{
self.sortingSegmentActionPressed = 1;
keyToSortBy = #"LastName";
}
//Create the sort descriptors
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:keyToSortBy ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
//Sort allSubItams by the values set in the sort descriptors
NSArray *sortedArray;
self.sortedArray = [allSubItems sortedArrayUsingDescriptors:sortDescriptors];
//Recreate the data structure by putting the newly sorted items into a dictionary sorted by inital letters.
NSDictionary *eachItemList; //A DICTIONARY FOR PUTTING ALL THE DATA FOR EACH ITEM IN IT'S OWN SECTION
NSMutableDictionary *tempSectionedDictionaryByFirstLetter = [[NSMutableDictionary alloc] init];
for (eachItemList in sortedArray) //eachElementList is a dictionary with a section for each item
{
NSDictionary *aDictionary = [[NSDictionary alloc] initWithDictionary:eachItemList];
NSString *firstLetterString;
firstLetterString = [[aDictionary valueForKey:keyToSortBy]substringToIndex:1];
NSMutableArray *existingArray;
if (existingArray = [tempSectionedDictionaryByFirstLetter valueForKey:firstLetterString])
{
[existingArray addObject:eachItemList];
} else {
NSMutableArray *tempArray = [NSMutableArray array];
[tempSectionedDictionaryByFirstLetter setObject:tempArray forKey:firstLetterString];
[tempArray addObject:eachItemList];
}
[aDictionary release];
[eachItemList release];
}
//Set the data source for the table (sectionedDictionaryByFirstLetter) to tempSectionedDictionaryByFirstLetter.
self.sectionedDictionaryByFirstLetter = tempSectionedDictionaryByFirstLetter;
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.sectionedDictionaryByFirstLetter allKeys] sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
[self.tableView reloadData];
[keyArray release];
[tempSectionedDictionaryByFirstLetter release];
}
Don't release eachItemList at the end of your loop. You did not explicitly allocate it in this context so you shouldn't release it.
The for (object in array) loop gives you a reference to the object in the array, not a copy. By sending a release message to this reference, you are decrementing the retain count of this object while it is still in the array. After a few times (depending on how many times the object has been retained, NSArray for example retains objects when they are added to the array) it's retain count will reach 0, and it will then become deallocated and you'll get crashes regarding unrecognised selectors or EXC_BAD_ACCESS and possibly other kinds of errors.