我想要一张tableview来显示我的车的状态,它只有7个单元格。并且值是通过网络获得的。当我获得值并设置单元格的标签文本时,它将崩溃。
@interface CarStatusController ()
@end
@implementation CarStatusController
- (instancetype)init {
self = [super init];
if (self) {
//init the data
_carStatuKey = [NSArray arrayWithObjects:@"Total_Km",@"Remain_L",@"Battery_V",@"Tyre_P",@"Cost_L",@"Door",@"Cover", nil];
_carStatuValue = [NSMutableArray new];
_resultData = [NSMutableData new];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[CarStatuCell class] forCellReuseIdentifier:@"CarStatuCell"];
[self postData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)postData {
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@DeviceInfo/GetVehicleStatus",API_PREFIX ]];
NSDictionary *parametersDic = @{@"VehicleID":@"10000"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [[UserLogin sharedInstance] LoginRequest];
NSString *token = [[UserLogin sharedInstance] getToken];
[request addValue:token forHTTPHeaderField:@"Authorization"];
[request addValue:[NSString stringWithFormat:@"aaron-clark-aic-%@",token] forHTTPHeaderField:@"Expect"];
[request addValue:@"Mobile" forHTTPHeaderField:@"DeviceType"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parametersDic options:NSJSONWritingPrettyPrinted error:nil]];
[request setHTTPMethod:@"POST"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
[self ParseData:data];
}
- (void)ParseData: (NSData *)data{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSDictionary *resultDic = [[dic objectForKey:@"Data"] objectAtIndex:0];
for (NSString *key in _carStatuKey) {
NSLog(@"value:%@",resultDic[key]);
[_carStatuValue addObject:resultDic[key]];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _carStatuName.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CarStatuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarStatuCell" forIndexPath:indexPath];
cell.statuNameLabel.text = _carStatuName[indexPath.row];
if (_carStatuValue.count == 0) {
cell.statuValueLabel.text = @"null";
} else {
cell.statuValueLabel.text = _carStatuValue[indexPath.row];//set value here will crash.
}
return cell;
}
@end
#import "CarStatuCell.h"
#include <Masonry.h>
@implementation CarStatuCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initSubViews];
[self setLayout];
}
return self;
}
- (void)initSubViews {
_statuNameLabel = [UILabel new];
[self.contentView addSubview:_statuNameLabel];
_statuValueLabel = [UILabel new];
_statuValueLabel.lineBreakMode = NSLineBreakByWordWrapping;
_statuValueLabel.numberOfLines = 0;
_statuValueLabel.font = [UIFont systemFontOfSize:13];
_statuValueLabel.text = @"Null";
[self.contentView addSubview:_statuValueLabel];
}
- (void)setLayout {
[_statuNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_statuNameLabel.superview.mas_left).with.offset(50);
make.centerY.equalTo(_statuNameLabel.superview.mas_centerY);
}];
[_statuValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_statuValueLabel.superview.mas_right).with.offset(-50);
make.centerY.equalTo(_statuValueLabel.superview.mas_centerY);
}];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000a1605
2016-07-08 10:34:19.660 ChangHongYema[4216:239738] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000a1605'
*** First throw call stack:
(
0 CoreFoundation 0x00000001062b8d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001059a6deb objc_exception_throw + 48
2 CoreFoundation 0x00000001062c1d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000106207cfa ___forwarding___ + 970
4 CoreFoundation 0x00000001062078a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010469c8f6 -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 84
6 UIKit 0x000000010469c747 -[UILabel textRectForBounds:limitedToNumberOfLines:] + 76
7 UIKit 0x00000001046a140d -[UILabel _intrinsicSizeWithinSize:] + 173
8 UIKit 0x00000001046a14e4 -[UILabel intrinsicContentSize] + 72
9 UIKit 0x0000000104d1808b -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 35
10 UIKit 0x0000000104d17d57 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 619
11 UIKit 0x0000000104d20e93 -[UIView(AdditionalLayoutSupport) updateConstraints] + 224
12 UIKit 0x00000001046a12c6 -[UILabel updateConstraints] + 256
13 UIKit 0x0000000104d201be -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 550
14 UIKit 0x0000000104d20489 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 198
15 UIKit 0x0000000104d200cd -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 309
16 Foundation 0x00000001039253d3 -[NSISEngine withBehaviors:performModifications:] + 155
17 UIKit 0x0000000104d1fadb -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58
18 UIKit 0x0000000104d2045d -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 154
19 UIKit 0x0000000104d200cd -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 309
20 UIKit 0x0000000104d20489 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 198
21 UIKit 0x0000000104d20ac1 __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 98
22 UIKit 0x0000000104d1fae4 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 67
23 UIKit 0x0000000104d2061e -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 254
24 UIKit 0x0000000104d21333 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 272
25 UIKit 0x00000001044d5a96 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
26 UIKit 0x00000001044e59a9 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 744
27 QuartzCore 0x0000000109268c00 -[CALayer layoutSublayers] + 146
28 QuartzCore 0x000000010925d08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
29 QuartzCore 0x000000010925cf0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
30 QuartzCore 0x00000001092513c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
31 QuartzCore 0x000000010927f086 _ZN2CA11Transaction6commitEv + 486
32 UIKit 0x000000010442572e _UIApplicationHandleEventQueue + 7135
33 CoreFoundation 0x00000001061de301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
34 CoreFoundation 0x00000001061d422c __CFRunLoopDoSources0 + 556
35 CoreFoundation 0x00000001061d36e3 __CFRunLoopRun + 867
36 CoreFoundation 0x00000001061d30f8 CFRunLoopRunSpecific + 488
37 GraphicsServices 0x000000010912bad2 GSEventRunModal + 161
38 UIKit 0x000000010442af09 UIApplicationMain + 171
39 ChangHongYema 0x00000001030455df main + 111
40 libdyld.dylib 0x000000010702492d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:0)
用以下代码替换你的cellforRow()方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CarStatuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarStatuCell" forIndexPath:indexPath];
cell.statuNameLabel.text = _carStatuName[indexPath.row];
if (_carStatuValue.count == 0) {
cell.statuValueLabel.text = @"null";
} else {
cell.statuValueLabel.text = [NSString stringWithFormate:@"%@",_carStatuValue[indexPath.row]];//set value here will crash.
}
return cell;
}
答案 1 :(得分:0)
您正在将值直接指定给文本。
在你的cellForRow中你可以做类似的事情:void HandleTagDeclDefinition(TagDecl *D) override
{
if(D->isStruct())
{
DeclContext *DC = static_cast<DeclContext *>(D);
for (DeclContext::decl_iterator DI = DC->decls_begin(), DEnd = DC->decls_end();
DI != DEnd; ++DI)
{
DI->dump();
}
}
}