在自定义单元格上显示字典中的数据时出现异常
Exception while showing data from dictionary on custom cell
我有一本字典,其中包含以下详细信息
{
Name = "American Airlines";
Picture = (
""
);
Rate = 3;
},
我想在单元格标签上显示名称...为此我正在执行以下代码:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDetail:) name:@"NewNotification" object:nil];
}
-(void)getDetail:(NSNotification *)notification
{
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
userInfo=dictionary;
[self.listView reloadData];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return [userInfo count];
return 115;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.Name.text =[userInfo valueForKey:@"Name"];
NSLog(@"the cell.Name.text is %@",cell.Name.text);
//[cell updateCell:userInfo];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
我不明白我在代码中做错了什么,因为它崩溃了,并且在 label.It 上没有显示任何内容,给出例外情况
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7bea2d20'"
请检查代码并告诉我哪里错了!
代码中存在解析问题。在你的 class 中取一个名为 arrList 的全局数组。
请更新代码
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
userInfo=dictionary;
[self.listView reloadData];
}
这个:
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
[arrList addObject: dictionary];
[self.listView reloadData];
}
当我们调用webservice时,array会添加字典。
现在更改表视图的代码行:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return [userInfo count];
return 115;
}
有了这个:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrList count];
}
我在 tableView 方法 cellForRowAtIndexPath:
中添加了几行代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(arrList.count > 0){
NSDictonary * dict = arrList[indexPath.row];
cell.Name.text =[dict valueForKey:@"Name"];
NSLog(@"the cell.Name.text is %@",cell.Name.text);
//[cell updateCell:userInfo];
}
return cell;
}
现在它将修复您的崩溃问题。
我有一本字典,其中包含以下详细信息
{
Name = "American Airlines";
Picture = (
""
);
Rate = 3;
},
我想在单元格标签上显示名称...为此我正在执行以下代码:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDetail:) name:@"NewNotification" object:nil];
}
-(void)getDetail:(NSNotification *)notification
{
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
userInfo=dictionary;
[self.listView reloadData];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return [userInfo count];
return 115;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.Name.text =[userInfo valueForKey:@"Name"];
NSLog(@"the cell.Name.text is %@",cell.Name.text);
//[cell updateCell:userInfo];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
我不明白我在代码中做错了什么,因为它崩溃了,并且在 label.It 上没有显示任何内容,给出例外情况
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7bea2d20'"
请检查代码并告诉我哪里错了!
代码中存在解析问题。在你的 class 中取一个名为 arrList 的全局数组。 请更新代码
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
userInfo=dictionary;
[self.listView reloadData];
}
这个:
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
[arrList addObject: dictionary];
[self.listView reloadData];
}
当我们调用webservice时,array会添加字典。
现在更改表视图的代码行:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return [userInfo count];
return 115;
}
有了这个:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrList count];
}
我在 tableView 方法 cellForRowAtIndexPath:
中添加了几行代码-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(arrList.count > 0){
NSDictonary * dict = arrList[indexPath.row];
cell.Name.text =[dict valueForKey:@"Name"];
NSLog(@"the cell.Name.text is %@",cell.Name.text);
//[cell updateCell:userInfo];
}
return cell;
}
现在它将修复您的崩溃问题。