0

我想在我的单元格中显示一个图像,但前提是JSON数据的值为 1(只有 0 或 1 可能)也许如果JSON数据的值为 0 显示不同的图像,所以每个单元格可以显示 2 个不同的图像.
到目前为止的问题是单元格包含图像,而不是JSON数据的值是什么。

到目前为止的代码:

NSArray *arrayOfEntry = [allDataDictionary objectForKey:@"notities"];
for (NSDictionary *diction in arrayOfEntry) 
{
    sonResults = [allDataDictionary objectForKey:@"notities"];
    NSMutableString *checkvink = [diction objectForKey:@"not_verwerkt"];

    if(![checkvink isEqual: @"1"])
    {
           NSString *path = [[NSBundle mainBundle] pathForResource:@"vink" ofType:@"png"];
           imageData = [NSData dataWithData:[NSData dataWithContentsOfFile:path]]; 
    } 
}    

然后我显示这样的图像

UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.notAfbeel.image = imageLoad;

我究竟做错了什么?

编辑:完成 cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
        static NSString *Cellidentifier = @"Cell";

NotitieCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier];

if(!cell)
{
    cell = [[NotitieCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
}

NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];

UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];

cell.notAfbeel.image = imageLoad;

return cell;
}

JSON数据

{
"notities": [{
    "not_nr": "1191555",
    "not_tijd": "12:29:54",
    "not_type": "0",
    "not_behandeld": "Richard",
    "not_bestemd": "Richard",
    "not_contactpers": "Maarten",
    "not_prioriteit": "1",
    "not_onderwerp": "Printer staat er nog steeds in",
    "not_naam": "apple store",
    "not_woonpl": "Amsterdam",
    "not_land": "NL",
    "not_verwerkt": "0"
    }
}
4

2 回答 2

0

也许您应该使用isEqualToString而不是isEqual,因为您在该行比较 NSString 值:

if(![checkvink isEqualToString: @"1"])
于 2013-05-31T10:19:34.337 回答
0

问题是这imageData是一个实例变量。您正在为整个对象实例设置其值,而不仅仅是单元格。所以,她cellForRowAtIndexPath被称为,它将这个变量值放在所有单元格中。

转换你arrayOfEntry的属性并假设它完全显示在你的 tableView 上,我们可以这样重写你cellForRowAtIndexPath

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     static NSString *Cellidentifier = @"Cell";

     NotitieCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier];

     if(!cell)
     {
         cell = [[NotitieCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
     }

     NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];

     NSMutableString *checkvink = [[self.arrayOfEntry objectAtIndex:indexPath.row] objectForKey:@"not_verwerkt"];

     NSData *imageData = nil; // local, now

     if(![checkvink isEqualToString: @"1"])
     {
         NSString *path = [[NSBundle mainBundle] pathForResource:@"vink" ofType:@"png"];
         imageData = [NSData dataWithData:[NSData dataWithContentsOfFile:path]];
     }

     UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];

     cell.notAfbeel.image = imageLoad;

     return cell;
  }

根据您的数据,这可能不是最佳答案,而只是给您一个想法,以便您可以适应它。

于 2013-05-31T11:27:30.507 回答