Utilizzando il TTLAUNCHERVIEW di tre20: hai bisogno di aiuto con la divisione e l'aggiunta di oggetti rimanenti da un NSMutableArray a un NSArray in Objective-C
-
28-09-2019 - |
Domanda
Ora sto usando questo codice:
- (void)loadLauncher:(NSMutableArray *)categoriesArray {
_launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
_launcherView.columnCount = 3;
// Number of pages in your launcherView.
NSMutableArray *pages = [[NSMutableArray alloc] initWithCapacity:2];
int numberOfObjects = [categoriesArray count];
// The launcherItems in each page, calculate automatically the number of objects available for the launcher.
NSMutableArray *launcherItems = [[NSMutableArray alloc] initWithCapacity:1];
// The counter to identify if the number of objects exceeds the,
// capacity of a launcher page of 9.
int j = 1;
for (int i = 0; i < numberOfObjects; i++){
if (j > 9){
// Add the current launcherItems array to the pages.
[pages addObject:launcherItems];
// Initialise new launcher items.
launcherItems = [[NSMutableArray alloc] initWithCapacity:1];
// Start the counter again.
j = 1;
} else {
int i = 0;
for (Category *c in categoriesArray) {
NSString *categoryImage = [[NSString stringWithFormat:@"bundle://category_%@_icon.png", [Utility removeSpecialCharacters:@"&'- " withString:c.categoryName]] lowercaseString];
NSLog(@" - %@", categoryImage);
TTLauncherItem *launcherItem = [[[TTLauncherItem alloc] initWithTitle:c.categoryName
image:categoryImage
URL:[NSString stringWithFormat:@"%d", i]
canDelete:NO] autorelease];
[launcherItems addObject:launcherItem];
i++;
}
}
j++;
}
// Add the current launcherItems to the pages.
[pages addObject:launcherItems];
[launcherItems release];
_launcherView.pages = pages;
[self.view addSubview:_launcherView];
}
Vecchio metodo:
Sto usando il TTLauncherView
controller da http://three20.info.
Tre20 è una raccolta di classi Objective-C che alimenta un numero crescente di applicazioni popolari sull'App Store. Fornisce dozzine di funzionalità incredibilmente utili che ti fanno risparmiare tempo di sviluppo.
La libreria è costruita per essere modulare, il che significa che è possibile incorporare selettivamente elementi della libreria nel progetto. C'è anche una serie crescente di estensioni tra cui l'analisi XML e JSON drop-in, nonché il supporto CSS Stylesheet per il loro numero di applicazioni.
Non sono del tutto sicuro di come fare quanto segue:
- Controlla se il mio
arrayOfLauncherItems
ha 16 oggetti in esso; e - Se ci sono più di 16 oggetti, aggiungi il resto degli oggetti rimanenti a
_launcherView.pages
. Quindi, se diciamo che ci sono un totale di 32 oggetti vorrei essere in grado di creare un altro array di 16 oggetti rimanenti e aggiungerli al_launcherView.pages
NSArray
.
Questo è un esempio di come il TTLauncherView
Il controller funziona:
TTLauncherView *_launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
NSMutableArray *arrayOfLauncherItems = [[NSMutableArray alloc] init];
//add TTLauncherItem objects to arrayOfLauncherItems.
_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, nil];
Il arrayOfLauncherItems
può contenere più di 16 oggetti, il che significa che il restante TTLauncherItem
Gli oggetti dovrebbero trovarsi nella seconda pagina e così via (a seconda di quanti oggetti totali ci sono).
Fare quanto segue ovviamente aggiunge gli stessi 16 oggetti da arrayOfLauncherItems
, il che significa che ora esiste una seconda pagina, che è essenzialmente ciò che voglio ottenere se ci sono più di 32 oggetti in arrayOfLauncherItems
.
_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, arrayOfLauncherItems, nil];
Soluzione
Ho il seguente codice che potresti voler utilizzare. L'idea di base è calcolare automaticamente il numero di pagine in base al numero di oggetti disponibili. Presumo che tu abbia 3x3 = 9 elementi di avvio in ogni pagina. In questo modo, non devi preoccuparti del numero totale di oggetti inferiore o superiore a 9. È possibile mettere questo valore in una costante se lo desideri.
NSMutableArray *pages = [NSMutableArray array];
NSMutableArray *launcherItems = [NSMutableArray array];
//the counter to identify if the number of objects exceeds the
//capacity of a launcher page of 9
int j = 1;
for (int i = 0; i < numberOfObjects; i++){
TTLauncherItem *launcherItem = [[[TTLauncherItem alloc] initWithTitle: @"a title"
image: @"bundle://abc.png"
URL: @"someUrlPath"
canDelete:TRUE] autorelease];
[launcherItems addObject:launcherItem];
j++;
if (j> 9){
//add the current launcherItems to the pages
[pages addObject:launcherItems];
//initialize new launcher items
launcherItems = [NSMutableArray array];
//start again the counter
j = 1;
}
}
//add the current launcherItems to the pages
[pages addObject:launcherItems];
_launcherView.pages = pages;
Altri suggerimenti
1) Usi [myArray count]
Per ottenere il numero di elementi in un array.
2) Usa un ciclo per:
NSMutableArray *overflow = [NSMutableArray array];
NSMutableArray *sixteen = [NSMutableArray array];
for (int i = 16; i < [arrayOfLauncherItems count]; i++)
{
[overflow addObject:[arrayOfLauncherItems objectAtIndex:i]];
}
for (int i = 0; i < 16; i++)
{
[sixteen addObject:[arrayOfLauncherItems objectAtIndex:i]];
}
_launcherView.pages = [NSArray arrayWithObjects:sixteen, overflow, nil];
Il primo loop aggiungerà gli oggetti dall'indice 16 fino alla fine dell'array e aggiungendoli a un altro array. Il secondo si esaurisce con un array dei primi 16 elementi dell'array originale.