-1

我有一个名为 invoiceInfo1 的数组,我将它传递给另一个名为 allInfo 的数组。

但我想要不同的 invoiceInfo 索引,创建不同的数组并将其传递给 allInfo。

我的代码如下:

for (int i=0; i<[userdata count]; i++) {  

    NSLog(@" userdata count :%d",[userdata count]);

    invoiceInfo1 = [NSMutableArray arrayWithObjects:[[userdata objectAtIndex:i]valueForKey:@"fname"],  [[userdata
    objectAtIndex:i]valueForKey:@"name"],

    [[userdata objectAtIndex:i]valueForKey:@"address"],

    [[userdata objectAtIndex:i]valueForKey:@"city"], nil];
    NSLog(@" info1 is:%@",invoiceInfo1); // invoiceInfo get overwrite when loop execute

    NSMutableArray* allInfo = [NSMutableArray arrayWithObjects:headers,invoiceInfo1 ,  nil]; 

    // HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to allInfo

}
4

1 回答 1

0

创建一个带有 header 和 info 数组的 NSObject 类:

@interface AllInfo: NSObject
{
    NSMutableArray *header;
    NSMutableArray *info;
}

@property (nonatomic, retain) NSMutableArray *header;
@property (nonatomic, retain) NSMutableArray *info;

@end

@implementation AllInfo

@synthesize header;
@synthesize info;

@end

然后实现这段代码:

allInfo = [[NSMutableArray alloc] init];
for (int i=0; i<[userdata count]; i++) {  

    NSLog(@" userdata count :%d",[userdata count]);

    NSMutableArray *invoiceInfo1 = [[NSMutableArray alloc] init]; 
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"fname"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"name"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"address"]];
    [invoiceInfo1 addObject:[userdata objectAtIndex:i]valueForKey:@"city"]];

    NSLog(@" info1 is:%@",invoiceInfo1); // invoiceInfo get overwrite when loop execute
    AllInfo *newInfo = [[AllInfo alloc] init];
    newInfo.header = headers;
    newInfo.info = invoiceInfo1;
    [allInfo addObject:newInfo]; 

    // HERE I Want Generate New Array of different index of invoiceInfo1 & pass it to    allInfo

}

希望这会有所帮助。

于 2012-08-06T11:33:00.680 回答