使用 Three20 的 TTLauncherView:需要帮助将剩余的对象从 NSMutableArray 拆分和添加到 Objective-C 中的 NSArray [英] Using Three20's TTLauncherView: Need help with splitting and adding remaining objects from an NSMutableArray to an NSArray in Objective-C

查看:45
本文介绍了使用 Three20 的 TTLauncherView:需要帮助将剩余的对象从 NSMutableArray 拆分和添加到 Objective-C 中的 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用此代码:

- (void)loadLauncher:(NSMutableArray *)categoriesArray {_launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];_launcherView.columnCount = 3;//launcherView 中的页面数.NSMutableArray *pages = [[NSMutableArray alloc] initWithCapacity:2];int numberOfObjects = [categoriesArray count];//每个页面中的launcherItems,自动计算启动器可用的对象数量.NSMutableArray *launcherItems = [[NSMutableArray alloc] initWithCapacity:1];//识别对象数量是否超过的计数器,//启动器页面的容量为 9.int j = 1;for (int i = 0; i  9){//将当前的 launcherItems 数组添加到页面中.[页面 addObject:launcherItems];//初始化新的启动项.launcherItems = [[NSMutableArray alloc] initWithCapacity:1];//再次启动计数器.j = 1;} 别的 {int i = 0;对于(类别 *c 中的类别数组){NSString *categoryImage = [[NSString stringWithFormat:@"bundle://category_%@_icon.png", [Utility removeSpecialCharacters:@"&'- " withString:c.categoryName]]lowercaseString];NSLog(@" - %@", categoryImage);TTLauncherItem *launcherItem = [[[TTLauncherItem alloc] initWithTitle:c.categoryName图像:类别图像URL:[NSString stringWithFormat:@"%d", i]canDelete:NO] 自动释放];[launcherItems addObject:launcherItem];我++;}}j++;}//将当前的 launcherItems 添加到页面中.[页面 addObject:launcherItems];[launcherItems 发布];_launcherView.pages = 页面;[self.view addSubview:_launcherView];}

旧方法:

我正在使用 http://three20.info 中的 TTLauncherView 控制器.>

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];//将 TTLauncherItem 对象添加到 arrayOfLauncherItems._launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, nil];

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

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

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

解决方案

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

NSMutableArray *pages = [NSMutableArray 数组];NSMutableArray *launcherItems = [NSMutableArray 数组];//识别对象数量是否超过的计数器//启动页面的容量为9int j = 1;for (int i = 0; i 9){//将当前的launcherItems添加到页面[页面 addObject:launcherItems];//初始化新的启动项launcherItems = [NSMutableArray 数组];//重新启动计数器j = 1;}}//将当前的launcherItems添加到页面[页面 addObject:launcherItems];_launcherView.pages = 页面;

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

解决方案

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;

这篇关于使用 Three20 的 TTLauncherView:需要帮助将剩余的对象从 NSMutableArray 拆分和添加到 Objective-C 中的 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆