0
votes

I am getting an error when changing the datasource of my TableView from an NSArray with objects(the example I found) to an NSMutableArray with the actual values I need to populate my table view with.

error:

lat is: 33, long is: -74, 2012-03-06 12:27:58.380 x[606:fb03] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6b64280 2012-03-06 12:27:58.381 x[606:fb03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6b64280'

the original way I was setting self.measurement and self.subinfo is commented out.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeasurementCell"];

switch (indexPath.section)
{
    case kMeasurement:
        cell.textLabel.text=[self.measurements objectAtIndex:indexPath.row];
        cell.detailTextLabel.text=[self.subinfo objectAtIndex:indexPath.row];
        break;
    default:
        cell.textLabel.text=@"Unkown";
}
return cell;
}

- (void)viewDidLoad
{
appdelegate = [[AppDelegate alloc]init];

//get lat
NSArray* fmArray = [appdelegate fetchEvents:@"Field_Measurement"];
NSMutableArray* latBindArray = [[NSMutableArray alloc]init];
NSMutableArray* longBindArray = [[NSMutableArray alloc]init];
for (Field_Measurement *fm in fmArray)
{
    [latBindArray addObject:fm.latitude];
    NSLog(@"lat is: %@", fm.latitude);
}

for (Field_Measurement *fm in fmArray)
{
    [longBindArray addObject:fm.longitude];
    NSLog(@"long is: %@", fm.longitude);
}

self.measurements = latBindArray;//[[NSArray alloc]initWithObjects:@"03/05/2012 @ 15:12", @"03/05/2012 @ 11:11", nil];
self.subinfo = latBindArray;//[[NSArray alloc]initWithObjects:@"Beta: 0.0234",@"Alpha: 3.977", nil];
[super viewDidLoad];
}

this is my first attempt at a table view(and new to objective-c all together) so any ideas of how to populate the table view from a NSMutableArray would be helpful. I have a sqlite database that I am pulling these records from if that helps.

thanks

2

2 Answers

2
votes

First thing is to not use your application delegate for getting data. Either get that using methods inside your VC or better yet, create a custom class (your model) to get the data you need and pass that back to your ViewController (your controller).

As far as populating the TableView from an Array (this assumes your Array is called myTableViewData):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [myTableViewData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // This assumes you are using Storyboard and have declared your Dynamic Prototype Cell Identifier
    OnCallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    NSDictionary *selRow = (NSDictionary *)[myTableViewData objectAtIndex:indexPath.row];
    // Do the rest of your processing here

    return cell;
}
1
votes

It appears you're trying to set cell.textLabel.text to an NSNumber instead of an NSString. self.measurements is populated with NSNumber objects, so you'll have to convert to string first.

Try changing these lines

cell.textLabel.text=[self.measurements objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[self.subinfo objectAtIndex:indexPath.row];

to these:

cell.textLabel.text=[NSString stringWithFormat:@"%@", [self.measurements objectAtIndex:indexPath.row]];
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@", [self.subinfo objectAtIndex:indexPath.row]];