我正在尝试在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方法中显示多个标签时,应用程序会刷新。如果您无法理解这个问题,请在评论中告诉我,我将提供更多信息。
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 length]: 无法识别的选择器发送到实例0xb000000000000083' ***第一次抛出调用堆栈:(0 CoreFoundation 0x00000001113a4c65 __exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000011103dbb7 objc_exception_throw + 45 2 CoreFoundation
0x00000001113ac0ad - [NSObject(NSObject)doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000011130213c ___转发_ + 988 4 CoreFoundation 0x0000000111301cd8 _CF_forwarding_prep_0 + 120 5 UIKit
0x0000000111970a74 - [UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 65 6 UIKit 0x00000001119708cf - [UILabel textRectForBounds:limitedToNumberOfLines:] + 76 7 UIKit
0x0000000111974379 - [UILabel _intrinsicSizeWithinSize:] + 170 8
UIKit 0x0000000111974459 - [UILabel intrinsicContentSize] + 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 updateConstraints] + 274
答案 0 :(得分:0)
我修复了错误似乎我必须更改从数组到字符串的值,因为它在NSNumber中出现,所以我通过将标签文本设置为NSString将其更改为NSString - 字符串格式如下:< / p>
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"]];
答案 1 :(得分:0)
0x00000001113ac0ad - [NSObject(NSObject)doesNotRecognizeSelector:]
这一行告诉我你正在向一个没有某种方法的对象发送消息。
- [NSCFNumber length]:无法识别的选择器发送到实例0xb000000000000083 2015-09-19 16:09:42.153 sRider [4995:102757] * 由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException& #39;,原因:&#39; - [__ NSCFNumber长度]:**
这个特别告诉我你正在向一些没有采用该方法的NSNumber对象发送length
方法。
[__NSCFNumber length]: unrecognized selector sent to instance 0x6d21350
您必须查看代码,看看您是否误将NSNumber误认为是NSString。看看你的JSON,并试着弄清楚你是否在自定义类中发送任何代表JSON的属性作为NSNumber,它们应该是NSString OR如果你错误地向NSNumber发送length
消息你真的想要一些其他的方法。