简体   繁体   English

设置UITableView从NSMutableArray错误

[英]setting UITableView from NSMutableArray error

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. 将我的TableView数据源从带有对象的NSArray(我找到的示例)更改为NSMutableArray时出现错误,而该值具有填充表视图所需的实际值。

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' lat is:33,long is:-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'

the original way I was setting self.measurement and self.subinfo is commented out. 我设置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:
        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. 这是我第一次尝试使用表视图(这是对Objective-c的新尝试),因此有关如何从NSMutableArray填充表视图的任何想法都将有所帮助。 I have a sqlite database that I am pulling these records from if that helps. 我有一个sqlite数据库,如果有帮助,我可以从中提取这些记录。

thanks 谢谢

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). 使用VC或更好的方法来获得该方法,或者创建一个自定义类(您的模型)以获取所需的数据,然后将其传递回ViewController(您的控制器)。

As far as populating the TableView from an Array (this assumes your Array is called myTableViewData): 至于从数组填充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;
}

It appears you're trying to set cell.textLabel.text to an NSNumber instead of an NSString. 似乎您正在尝试将cell.textLabel.text设置为NSNumber而不是NSString。 self.measurements is populated with NSNumber objects, so you'll have to convert to string first. self.measurements填充了NSNumber对象,因此您必须首先转换为字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM