Ios 在自定义单元格上显示字典中的数据时发生异常
我有一本字典,里面有以下的细节Ios 在自定义单元格上显示字典中的数据时发生异常,ios,dictionary,custom-cell,Ios,Dictionary,Custom Cell,我有一本字典,里面有以下的细节 { Name = "American Airlines"; Picture = ( "" ); Rate = 3; }, 我想在单元格的标签上显示名称…我正在为其执行以下代码: -(void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector
{
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;
}
我无法理解我在代码中做错了什么,因为它崩溃了,并且在标签上没有显示任何内容。它给出了异常
“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[\uu NSArrayI length]:未识别的选择器已发送到实例0x7bea2d20'”
请检查代码并告诉我哪里出错了 代码中存在解析问题。在类中使用一个名为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];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrList count];
}
当我们调用webservice时,数组将添加字典
现在更改tableview的代码行:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return [userInfo count];
return 115;
}
关于这一点:
if([notification.name isEqualToString:@"NewNotification"])
{
NSDictionary *dictionary=notification.userInfo;
NSLog(@"Dictionary %@",dictionary);
[arrList addObject: dictionary];
[self.listView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrList count];
}
我在tableView方法CellForRowatineXpath中添加了几行代码:
-(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;
}
现在,它将修复您的崩溃。添加异常断点并查看崩溃的位置。要知道如何添加异常断点,您可以查看。这很容易做到。我尝试了这段代码,但它没有将字典的任何元素添加到arrlist中,并将arrlist显示为nil,而在字典中显示117个值。您是否可以向我显示117个值的屏幕截图或响应…?抱歉,无法共享字典,但它返回117个值,如{Name=“美国航空公司”;Picture=(“”);Rate=3;}好的,np…请添加NSLogs以查看这些dict是否正确添加…在这种情况下:if([notification.name IsequalString:@“NewNotification”]){我已经将字典和arrlist都记录下来了……字典中填充了117个元素,但是这些元素没有添加到arrlist中,因此arrlist为空。