我现在正在使用此代码:
- (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.info中的TTLauncherView
控制器。
Three20是Objective-C类的集合,为App Store上越来越多的流行应用程序提供支持。它提供了许多非常有用的功能,可以节省您的开发时间。
库是模块化的,这意味着您可以选择性地将库的元素合并到项目中。还有一组不断增加的扩展,包括drop-in XML和JSON解析,以及用于主题应用程序的CSS样式表支持。
我不太确定如何执行以下操作:
arrayOfLauncherItems
中是否有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个对象,这基本上就是我想要实现的。 / p>
_launcherView.pages = [NSArray arrayWithObjects:arrayOfLauncherItems, arrayOfLauncherItems, nil];
答案 0 :(得分:1)
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个元素的数组。
答案 1 :(得分: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;