2012-03-06 81 views
0

当我的TableView的数据源从具有对象的NSArray(我发现的例子)更改为NSMutableArray与我需要填充我的表视图的实际值时,我得到一个错误。设置从NSMutableArray的UITableView错误

错误:

LAT是:33, 长为:-74, 2012-03-06 12:27:58.380 X [606:FB03] - [__ NSCFNumber isEqualToString:]:无法识别的选择发送到实例0x6b64280 2012-03-06 12:27:58.381 x [606:fb03] *由于未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x6b64280'

我设置self.measurement和self.subinfo的原始方式被注释掉了。

- (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: 
     [email protected]"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]; 
} 

这是我在一个表视图第一次尝试(和新的Objective-C一起),所以如何填充从NSMutableArray的表视图将是有益的任何想法。如果有帮助,我有一个sqlite数据库,我拉这些记录。

感谢

回答

1

看来你要设置cell.textLabel.text到一个NSNumber,而不是一个NSString。使用NSNumber对象填充self.measurements,因此您必须先将其转换为字符串。

尝试改变这些线

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

这些:

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

完蛋了,我想这是一个铸造的问题,但由于某些原因的.text没有点击。感谢亚伦 – BamBamBeano 2012-03-06 18:07:16

2

第一件事情是不使用你的应用程序委托获取数据。要么在VC中使用方法,要么使用更好的方法,创建一个自定义类(您的模型)来获取所需的数据,并将其传递回您的ViewController(您的控制器)。

至于从数组填充的TableView(这里假设你的数组称为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; 
}