if语句在单元格中显示图像

时间:2013-05-31 10:13:02

标签: json uitableview uiimage

我想在单元格中显示图像,但前提是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"
    }
}

2 个答案:

答案 0 :(得分:0)

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

if(![checkvink isEqualToString: @"1"])

答案 1 :(得分: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;
  }

根据您的数据,这可能不是最佳答案,但只是为了给您一个想法,以便您可以对其进行调整。

相关问题