Deep
Deep

Reputation: 33

Issue in adding dynamic cell in table view?

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

Upvotes: 1

Views: 628

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

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.

Upvotes: 1

Amit Shah
Amit Shah

Reputation: 4164

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];

Upvotes: 0

lawicko
lawicko

Reputation: 7344

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.

Upvotes: 1

Related Questions