尝试从来自 Web 服务 ios 的响应中显示多个标签

Posted

技术标签:

【中文标题】尝试从来自 Web 服务 ios 的响应中显示多个标签【英文标题】:Trying to show to multiple label from response which came from web service ios 【发布时间】:2015-09-19 09:43:13 【问题描述】:

我正在尝试在 tableview 中显示来自 web 服务响应的驱动程序详细信息(用于复杂设计的自定义 tableview 单元格)但是应用程序崩溃了,我尝试了许多其他替代方法但不起作用请帮助这里是代码:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
    NSArray * jsonResponseDataArray = [jsonResponseData objectForKey:@"data"];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^
        [self createMarkerObjectsWithJson:jsonResponseDataArray];
    ];

    [[self driverListTableView] reloadData];


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    return 1;

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return [arrayMain count];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *cellIdentifier = @"Cell";
    //NSLog(@"arrayMain : %@",arrayMain);
    //method to return custom cell
    DriverListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DriverListTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    
    //NSLog(@"nsarray: %lu",(unsigned long)[arrayMain count]);
    NSDictionary *testDict = [arrayMain objectAtIndex:indexPath.row];
    NSLog(@"mainDict : %@",testDict);
    NSString *firstName = [testDict objectForKey:@"f_name"];
    NSString *lastName = [testDict objectForKey:@"l_name"];
    NSString  *companyName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
    NSString *basePrice = [testDict objectForKey:@"base_price"];
    NSString *carType = [testDict objectForKey:@"car_type_label"];
    NSString *waitingTime = [testDict objectForKey:@"waiting_time"];
    NSLog(@"d_id %@",companyName);
    cell.labelCompanyName.text = companyName;
    //cell.imageView.image = [UIImage imageNamed:@"driver-1.png"];
    cell.labelBasePrice.text = basePrice;
    cell.labelCarType.text = carType;
    cell.labelWaitingTime.text = waitingTime;
    cell.backgroundColor = [UIColor clearColor];
    return cell;


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    NSLog(@"selected at index: %ld",(long)indexPath.row);


@end

当我尝试在 cellForRowAtindexpath 方法中显示多个标签时,应用程序会崩溃。如果您无法理解问题,请在 cmets 中告诉我,我会提供更多信息。

jsonResponseData 是:


    count = 131;
    data =     (
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 1;
            "car_type_label" = "";
            color = "";
            "d_id" = bZ;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 8;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 2;
            "car_type_label" = "";
            color = "";
            "d_id" = KlCxEa;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 20;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = bZHXHX;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sourav;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 22;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlbZCx;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sourav;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 21;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlbZKl;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sourav;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 15;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlbZpS;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 22;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlpSQv;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 15;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlpSHX;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sudipta;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 7;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlEabZ;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 6;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlQvEa;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 14;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = KlHXmN;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Brando;
            status = 1;
            "waiting_time" = 8;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNbZCx;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Brando;
            status = 1;
            "waiting_time" = 6;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNbZEa;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 16;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNmNEa;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 23;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNmNbG;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sudipta;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 20;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNOrKl;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Brando;
            status = 1;
            "waiting_time" = 9;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNOrbG;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 22;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNEamN;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sudipta;
            "l_name" = Roy;
            status = 1;
            "waiting_time" = 28;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = mNEapS;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Singh;
            status = 1;
            "waiting_time" = 24;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrCxKl;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 30;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrCxmN;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Roy;
            status = 1;
            "waiting_time" = 7;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrmNbZ;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Roy;
            status = 1;
            "waiting_time" = 24;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrmNQv;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Vasu;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 19;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrOrCx;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 19;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrOrmN;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Saikat;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 22;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrEabZ;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Sourav;
            "l_name" = Khan;
            status = 1;
            "waiting_time" = 12;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrHXbZ;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Brando;
            status = 1;
            "waiting_time" = 25;
        ,
                
            "base_fare" = 20;
            "c_no" = "";
            "c_type" = 3;
            "car_type_label" = "";
            color = "";
            "d_id" = OrHXHX;
            "d_type" = 1;
            "driver_type_label" = "";
            "f_name" = Amar;
            "l_name" = Prasad;
            status = 1;
            "waiting_time" = 29;
        
    );

错误

-[NSCFNumber 长度]:无法识别的选择器发送到实例 0xb000000000000083 2015-09-19 16:09:42.153 sRider[4995:102757] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSCFNumber 长度]: 无法识别的选择器发送到实例 0xb000000000000083' *** 第一次抛出调用堆栈:( 0 CoreFoundation 0x00000001113a4c65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000011103dbb7 objc_exception_throw + 45 2 核心基础 0x00000001113ac0ad -[NSObject(NSObject) 不识别选择器:] + 205 3 核心基础 0x000000011130213c ___forwarding_ + 988 4 CoreFoundation 0x0000000111301cd8 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000111970a74 -[UILabel _textRectForBounds:limitedToNumberOfLines:包括Shadow:] + 65 6 UIKit 0x00000001119708cf -[UILabel textRectForBounds:limitedToNumberOfLines:] + 76 7 UIKit 0x0000000111974379 -[UILabel _intrinsicSizeWithinSize:] + 170 8 UIKit 0x0000000111974459-[UILabel 内在内容大小] + 76 9 UIKit 0x0000000111e12a0c -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 33 10 UIKit 0x0000000111e127d0 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 422 11 UIKit 0x0000000111e19bc5 -[UIView(AdditionalLayoutSupport) updateConstraints] + 162 12 UIKit 0x00000001119742c4 -[UILabel 更新约束] + 274

【问题讨论】:

这是很多代码,没有关于崩溃类型、消息或堆栈跟踪的详细信息。仅显示适当的代码和所有崩溃详细信息 同意@Wain。我会测试填充您正在调用的数组的对象,以在带有标签的简单视图中加载 tableView,以确保您没有调用不存在的对象。然后我会检查手机注册以确保你做得对。还要测试数组以确保您没有尝试将 tableView 填充到 array.count 之后。 我已经编辑了代码以使其简短,@noobsmcgoobs 我无法真正理解你的意思我是 Objective c 的新手,所以请不要让我扯掉我的头发。 【参考方案1】:

我修复了错误,好像我必须将我从数组中获取的值更改为字符串,因为它来自 NSNumber ,所以我通过将标签文本设置为 NSString 将其更改为 NSString - 格式如下的字符串:

cell.labelBasePrice.text = [NSString stringWithFormat:@"%@",[testDict objectForKey:@"base_fare"]];
    cell.labelCarType.text = [NSString stringWithFormat:@"%@",[testDict objectForKey:@"car_type_label"]];
    cell.labelWaitingTime.text = [NSString stringWithFormat:@"Waiting %@",[testDict objectForKey:@"waiting_time"]];

【讨论】:

感谢@Wain 指出我忘记检查控制台中的错误,因为我是菜鸟,很难习惯这种东西。 :P【参考方案2】:

0x00000001113ac0ad -[NSObject(NSObject) doesNotRecognizeSelector:]

这一行告诉我,您正在向一个对象发送消息,该对象的标头中没有某些方法。

-[NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000083 2015-09-19 16:09:42.153 sRider[4995:102757] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '-[__NSCFNumber 长度]:**

这个特别告诉我你正在向一些不采用该方法的 NSNumber 对象发送length 方法。

[__NSCFNumber length]: unrecognized selector sent to instance 0x6d21350

您必须检查您的代码,看看您是否将 NSNumber 误认为 NSString。查看您的 JSON 并尝试确定您是否将任何表示自定义类中的 JSON 的属性作为 NSNumber 发送,它们应该是 NSString 或者如果您错误地将 length 消息发送到真正的 NSNumber你想要一些其他的方法。

【讨论】:

@noobsmgoobs 我已经回答了我的问题,谢谢你的回答

以上是关于尝试从来自 Web 服务 ios 的响应中显示多个标签的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Ios 中解析 Web 服务的响应?

为啥当我在 ios 应用程序中使用 alamofire 时来自服务器的字符串响应会发生变化?

iOS 检索 Web 响应数据后如何从 JSON 中检索值

如何在 iOS 7 中解析从 .NET Web 服务收到的 JSON 响应

iOS:显示来自 JSON 的 svg

iOS - 来自 NSData 完成处理程序的 writeToFile