0

How do i use indexPath.row to get an object from an array? I have tried with the following code but it returns "signal SIGABRT".. Please help

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *inde = [NSString stringWithFormat:@"%d", indexPath.row];
    NSNumber *num = [NSNumber numberWithInteger: [inde integerValue]];
    int intrNum = [num intValue];
    NSString *name = [basket objectAtIndex:intrNum];


    cell.textLabel.text = name;
    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    basket = [[NSMutableArray alloc] init];
    [basket addObject:@"1"];
    [self makeGrid];
 }


- (void)addToBasket:(id)sender {
    NSInteger prodID = ((UIControl*)sender).tag;

    [basket insertObject:[NSNumber numberWithInt:prodID] atIndex:0];
    [self.tableView reloadData];
}

Error message:

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080 2012-05-05 02:29:18.208 app[7634:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080'
2
  • Are you sure basket is not nil? Commented May 5, 2012 at 0:29
  • yes. But it works when i run the app, but when i try to update the table by calling [self.tableView reloadData]; i get the SIGABRT Commented May 5, 2012 at 0:33

3 Answers 3

3

Why don't you just use

NSString *name = [basket objectAtIndex:indexPath.row];

?

Sign up to request clarification or add additional context in comments.

3 Comments

well, it works when i run the app, but when i call [self.tableView reloadData]; it does not..
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080 2012-05-05 02:29:18.208 app[7634:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080'
and also edit your question with the code where you instantiate you NSArray
1

In addToBasket method you put NSNumber object into basket array, but in cellForRowAtIndexPath method you expect NSString objects in basket. To make your code working you can use safe conversion to string:

NSString *name = [NSString stringWithFormat:@"%@",[basket objectAtIndex:intrNum]];

Comments

0

When your code

int intrNum = [num intValue];

the intrNum may is not real IndexPath Integer it may return the kind of address (example like '88792' or etc.) . So, It will cause problem with your array

Correct your get object from array code in cellForRowAtIndexPath like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

/* NSString *inde = [NSString stringWithFormat:@"%d", indexPath.row];
NSNumber *num = [NSNumber numberWithInteger: [inde integerValue]];
int intrNum = [num intValue]; */

NSString *name = [basket objectAtIndex:indexPath.row];


cell.textLabel.text = name;
return cell;

}

and in the addToBasket you should not use numberWithInt like the reason about int that I've mentioned above.

- (void)addToBasket:(id)sender {
    NSInteger prodID = ((UIControl*)sender).tag;

    // [basket insertObject:[NSNumber numberWithInt:prodID] atIndex:0];
    [basket insertObject:[NSNumber numberWithInteger:prodID] atIndex:0];    

    [self.tableView reloadData];
}

Hope it helps you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.