【问题标题】:setting UITableView from NSMutableArray error从 NSMutableArray 错误设置 UITableView
【发布时间】:2012-03-06 17:41:54
【问题描述】:

将 TableView 的数据源从带有对象(我找到的示例)的 NSArray 更改为带有填充表格视图所需的实际值的 NSMutableArray 时出现错误。

错误:

纬度是: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:
        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];
}

这是我第一次尝试使用表视图(并且是 Objective-c 的新手),因此任何关于如何从 NSMutableArray 填充表视图的想法都会有所帮助。我有一个 sqlite 数据库,如果有帮助,我会从中提取这些记录。

谢谢

【问题讨论】:

    标签: iphone ios xcode sqlite uitableview


    【解决方案1】:

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

    至于从 Array 填充 TableView(假设您的 Array 称为 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;
    }
    

    【讨论】:

      【解决方案2】:

      您似乎正在尝试将cell.textLabel.text 设置为 NSNumber 而不是 NSString。 self.measurements 填充了 NSNumber 对象,因此您必须先转换为字符串。

      尝试更改这些行

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

      【讨论】:

      • 就是这样,我认为这是一个投射问题,但由于某种原因,.text 没有点击。谢谢亚伦
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多