gpt4 book ai didi

ios - 在自定义单元格上显示字典中的数据时出现异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:02:53 24 4
gpt4 key购买 nike

我有一本包含以下详细信息的字典

{
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;

}

我无法理解我在代码中做错了什么,因为它崩溃了并且在标签上没有显示任何内容。它给出了异常

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 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];

}

当我们调用webservice时,数组会添加字典。

现在更改tableview的代码行:

-(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;

现在它将修复您的崩溃。

关于ios - 在自定义单元格上显示字典中的数据时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com