首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Three20 20的TTLauncherView:需要帮助将NSMutableArray中的剩余对象拆分并添加到objects中的NSArray中

使用Three20 20的TTLauncherView:需要帮助将NSMutableArray中的剩余对象拆分并添加到objects中的NSArray中
EN

Stack Overflow用户
提问于 2010-11-26 13:33:55
回答 2查看 1.3K关注 0票数 0

我现在使用以下代码:

代码语言:javascript
复制
- (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];
}

旧方法:

我正在使用来自TTLauncherViewhttp://three20.info控制器。

Three20是Objective类的集合,它为App上越来越多的流行应用程序提供了动力。它提供了数十个非常有用的特性,为您节省了开发时间。

库是模块化的,这意味着您可以有选择地将库的元素合并到您的项目中。还有越来越多的扩展,包括插入XML和JSON解析,以及对应用程序主题化的CSS样式表支持。

我不太清楚如何做到以下几点:

  1. 检查我的arrayOfLauncherItems中是否有16个对象;如果有超过16个对象,则
  2. 将其余的对象添加到_launcherView.pages中。因此,如果我们假设总共有32个对象,我希望能够为剩下的16个对象创建另一个数组并将它们添加到NSArray.

这是TTLauncherView控制器工作方式的一个示例:

代码语言:javascript
复制
TTLauncherView *_launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];

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

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

arrayOfLauncherItems可能包含超过16个对象,这意味着其余的TTLauncherItem对象应该在第二个页面上,依此类推(取决于总共有多少个对象)。

执行以下操作显然会从arrayOfLauncherItems中添加相同的16个对象,这意味着现在有了第二个页面,如果arrayOfLauncherItems中有超过32个对象,这基本上就是我想要实现的。

代码语言:javascript
复制
_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, arrayOfLauncherItems, nil];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-06 14:05:07

我有下面的代码,您可能想要使用。基本思想是根据可用对象的数量自动计算页面数。我假设每个页面中都有3x3=9启动项。这样,您就不必担心小于或大于9的对象的总数。如果需要,可以将该值设为常量。

代码语言:javascript
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2010-11-26 13:47:36

1)使用[myArray count]获取数组中的项目数。

2)使用for循环:

代码语言:javascript
复制
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];

第一个for循环将对象从索引16添加到数组的末尾,并将它们添加到另一个数组中。第二个以原始数组的前16个元素的数组结束。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4285722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档