views:

70

answers:

4

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..

A: 

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 :)

willcodejavaforfood
thanks alot, it works now, and I'm extremely happy =)
dav3
+5  A: 

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!

Stephen Darlington
thank you =) it works now... made me happy =)
dav3
A: 

myArray is proberbly autoreleased. Use self.myArray to set the sorted array. (with the correct @property (retain) )

Sander Backus
thank you, it works =) I'm happy =)
dav3
A: 

If myArray is an instance variable instead of a property, try:

myArray = [[resultArray sortedArrayUsingDescriptors:sortDescriptors] retain];
BarrettJ