【问题标题】:Using Three20's TTLauncherView: Need help with splitting and adding remaining objects from an NSMutableArray to an NSArray in Objective-C使用 Three20 的 TTLauncherView:在 Objective-C 中将剩余对象从 NSMutableArray 拆分和添加到 NSArray 时需要帮助
【发布时间】:2010-11-26 13:33:55
【问题描述】:

我现在正在使用这个代码:

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

旧方法:

我正在使用来自http://three20.infoTTLauncherView 控制器。

Three20 是一个 Objective-C 类的集合,它为 App Store 上越来越多的流行应用程序提供支持。它提供了许多非常有用的功能,可以节省您的开发时间。

该库是模块化构建的,这意味着您可以有选择地将库的元素合并到您的项目中。还有一组不断增长的扩展,包括插入式 XML 和 JSON 解析,以及用于主题化应用程序的 CSS 样式表支持。

我不太确定如何执行以下操作:

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

这是TTLauncherView 控制器如何工作的示例:

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 个对象,这基本上是我想要实现的。

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

【问题讨论】:

    标签: objective-c nsmutablearray nsarray three20 ttlauncherview


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 我已经更新了我原来的问题并使用了你的代码。但是我在两个页面上都没有得到相同的项目。还将页数的 initWithCapacity 设置为 2。如果项目总数超过两页,我该如何控制?
    • 你能帮我解决这个错误吗?我正在使用您的代码,但我现在使用的 columnCount 为 3,每页总共有 9 个类别。问题是第 10 个项目没有显示。它只是跳过第 10 个项目并移至第 2 页,但它从第 11 个类别项目开始。你介意帮我解决这个问题吗?
    • 您好 Fulvio,抱歉耽搁了,我不知道您的评论。如果有效,请查看更新的代码。
    【解决方案2】:

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

    2) 使用 for 循环:

    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 个元素组成的数组结束。

    【讨论】:

    • 以上代码没有按我需要的方式工作。它实际上比它应该的要多得多。它实际上每页显示超过 16 个,但它现在确实在第二页上显示了一些。也许我没有正确实施它。我正在检查 IF arrayOfLauncherItems 是否大于 16 并使用您的代码 ELSE 只需执行 _launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, nil];
    • 我想我明白你想要做什么。请参阅我编辑的代码示例。
    • 我认为您忘记替换其中的几个数组。我不太确定 myArray 和 arrayOfLauncherItems 现在是什么。他们应该是一样的吗?你也没有对溢出做任何事情。
    • 我确实不小心混合了变量名。请参阅代码示例(再次)。我需要咖啡。
    • 哇!?最初,我什至没有尝试通过按住其中一项来实际移动对象。现在看来,当我什至没有实现您的代码但每页有超过 12 个项目时,按住并移动其中一个项目然后放手。它实际上会自动将屏幕外的所有项目推送到下一页。我认为这可能是一个问题,因为我正在使用 tabBarController 并且 TTLauncherView 在第一次加载视图时不知道它应该因为标签栏而正确分页。嗯,这很奇怪。它只会在我移动项目后修复它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多