问题是------我从url获取数据和arrayobj中的数据。当我运行此项目时,我收到此错误。我在项目中做错了语句或代码请检查{{1 }和connectiondidfinishloading
。
cellforrowanindexpath
//这是索引路径代码
的行的单元格- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];
newsobj=[dictobj objectForKey:@"news"];
//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];
for (NSDictionary *dict in newsobj)
{
NSDictionary *title=[dict objectForKey:@"title"];
[arrayobj addObject:title];
}
[[self tableobj]reloadData];
}
错误是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
[[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text=[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"];
return newcustomobj;
}
答案 0 :(得分:5)
您假设的变量dictobj是NSDictionary,实际上它是NSString。由于字符串不响应objectForKey:方法,因此您的应用程序崩溃了。这就是错误消息告诉你的内容。检查此link。了解更多细节。
答案 1 :(得分:1)
您解决的其中一个对象(您认为它是字典)实际上是一个字符串。
特别是在解析JSON或XML时:始终仔细检查您获取并继续使用的对象的类。使用[object isKindOfClass:[ClassIThinkItIs class]]
答案 2 :(得分:0)
cell.lblTitle.text = [[dataObjectArray objectAtIndex:indexPath.row]title];
cell.datobj.text = [[dataObjectArray objectAtIndex:indexPath.row]date];
cell.longdicobj.text = [[dataObjectArray objectAtIndex:indexPath.row]long_desc];
[cell.imageobj setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dataObjectArray objectAtIndex:indexPath.row]image_file]]]
placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];
答案 3 :(得分:0)
我认为json数据返回是news
密钥字典的新闻字典数组,每个news
字典都有一个title
密钥。因此,您可以尝试使用以下代码在title
中显示cellForRow
:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];
newsobj=[dictobj objectForKey:@"news"];
//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];
for (NSDictionary *dict in newsobj)
{
NSString *title=[dict objectForKey:@"title"]; //title may be NSString type
[arrayobj addObject:title];
}
[[self tableobj] reloadData];
}
在CellForRow中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
[[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text= [arrayobj objectAtIndex:0]; //just get title of first news, you can replace 0 with indexpath.row to show news for row.
return newcustomobj;
}