UITableView中的二维数组

时间:2014-09-29 10:21:29

标签: ios objective-c uitableview multidimensional-array nsmutablearray

我从JSON获得image和imageTitle,如下所示

//geting imageTitle
NSString*titles =[dealPosted valueForKey:@"deaTitle"];
NSLog(@"titles^^^%@",titles);
[titlearray addObject:titles]; 

//geting Images
NSString *geting2 =[diction2 valueForKey:@"dealimage"];
NSData *getdata2=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting2]];
NSLog(@"good day2 %@",getdata2);
dataimages2=[UIImage imageWithData:getdata2];
[secondarray addObject:dataimages2];

然后我将它们传递给UITableViewCell,就像这样

cell.thumbnailImageView.image = [secondarray objectAtIndex:indexPath.row];
cell.nameLabel.text = [titlearray objectAtIndex:indexPath.row];

但这需要使用两个mutablearray。我认为它需要很大的尺寸。所以请任何人告诉我在这种情况下如何使用单个数组(如两个Dimension数组)。

请帮帮我。

感谢高级。

2 个答案:

答案 0 :(得分:1)

    NSMutableDictionary *dataDic  =[[NSMutableDictionary alloc] init];

    NSString*titles =[dealPosted valueForKey:@"deaTitle"];
    [dataDic setValue:titles forKey:@"deaTitle"];


    NSString *geting2 =[diction2 valueForKey:@"dealimage"];
    NSData *getdata2=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting2]];
    NSLog(@"good day2 %@",getdata2);
    dataimages2=[UIImage imageWithData:getdata2];
    [dataDic setValue:dataimages2 forKey:@"dealimage"];

    [titlearray addObject:titles];

并将它们传递到UITableViewCell,然后你的问题就解决了

  cell.thumbnailImageView.image = [[titlearray objectAtIndex:indexPath.row] valueForKey:@"dealimage"];
    cell.nameLabel.text = [[titlearray objectAtIndex:indexPath.row] valueForKey:@"deaTitle"];

答案 1 :(得分:0)

您可以将这两个值一个接一个地设置在一个数组中(对于每个图像)。然后可以作为indexPath.row * 2访问它们。

//geting imageTitle
NSString*titles =[dealPosted valueForKey:@"deaTitle"];
            NSLog(@"titles^^^%@",titles);
            [myArray addObject:titles]; 
 //geting Images
NSString *geting2 =[diction2 valueForKey:@"dealimage"];
            NSData *getdata2=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting2]];
            NSLog(@"good day2 %@",getdata2);
            dataimages2=[UIImage imageWithData:getdata2];
            [myArray addObject:dataimages2];

现在在表委托方法numberOfRowsInSection: return myArray.count/2

并在cellForRowAtIndexPath中:

cell.thumbnailImageView.image = [myArray objectAtIndex:(indexPath.row*2)+1];
        cell.nameLabel.text = [myArray objectAtIndex:indexPath.row*2];

我之所以回答你,只是因为你使用一个阵列要求它,但实际上它并没有通过使用单个2D阵列,包含所有信息的单个1D阵列或2个独立的1D阵列对你的记忆产生任何影响。