Is there a way to duplicate the number of NSDictio

2019-09-01 03:52发布

问题:

I currently have this array of NSDictionarys in my table view, but I need to duplicate the number of dictionaries in the array as the table view scrolls down. Is there a way to achieve this, and also specify or know what the dictionary names will be?

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

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

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

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

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


objects = [NSMutableArray arrayWithObjects:d0, d1, d2, d3, d4, nil];

回答1:

First of all, it seams to be easier to create the dictionaries in a loop:

NSMutableArray *objects = [NSMutableArray new];
for( int i = 0; i < 5; i++ )
{
   [objects addObject:[NSMutableDictionary new]];
}

If you want to "duplicate" the dictionaries, you can do that with a loop, too:

for( NSMutableDictionary *dictionary in [objects copy])
{
  [objects addObject:[dictionary mutableCopy]];
}

But I cannot get rid of the feeling that your whole approach is wrong. What is the idea behind that?