Using Three20's TTLauncherView: Need help with splitting and adding remaining objects from an NSMutableArray to an NSArray in Objective-C

不羁岁月 提交于 2019-12-25 01:52:38

问题


I am now using this code:

- (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];
}

Old method:

I am using the TTLauncherView controller from http://three20.info.

Three20 is a collection of Objective-C classes that powers a growing number of popular applications on the App Store. It provides dozens of incredibly useful features that save you development time.

The library is built to be modular, which means you can selectively incorporate elements of the library into your project. There is also a growing set of extensions including drop-in XML and JSON parsing, as well as CSS stylesheet support for theming your applications.

I am not quite sure how to do the following:

  1. Check whether my arrayOfLauncherItems has 16 objects in it; and
  2. If there are more than 16 objects, add the rest of the remaining objects to _launcherView.pages. So if let's say there's a total of 32 objects I'd want to be able to create another array of the remaining 16 objects and add them to the _launcherView.pages NSArray.

This is an example of how the TTLauncherView controller works:

TTLauncherView *_launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];

NSMutableArray *arrayOfLauncherItems = [[NSMutableArray alloc] init];
//add TTLauncherItem objects to arrayOfLauncherItems.

_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, nil];

The arrayOfLauncherItems may contain more than 16 objects, which means that the remaining TTLauncherItem objects should be on the second page and so forth (depending on how many total objects there are).

Doing the following obviously adds the same 16 objects from arrayOfLauncherItems, which means that there's now a second page, which is essentially what I want to achieve if there's more than 32 objects in arrayOfLauncherItems.

_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, arrayOfLauncherItems, nil];

回答1:


I have the following code that you may want to use. Basic idea is to calculate automatically the number of pages based on the number of objects available. I assume that you have 3x3=9 launcher items in each page. In this way, you don't have to worry about the total number of objects less than or greater than 9. You can put this value in a constant if you want.

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;



回答2:


1) You use [myArray count] to get the number of items in an array.

2) Use a for loop:

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];

The first for loop would add the objects from index 16 until the end of the array and add them to another array. The second ends up with an array of the first 16 elements of the original array.



来源:https://stackoverflow.com/questions/4285722/using-three20s-ttlauncherview-need-help-with-splitting-and-adding-remaining-ob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!