iPhone表格视图:制作章节,UILocalizedIndexedCollat​​ion选择器

时间:2012-11-01 20:50:40

标签: iphone objective-c uitableview

我在UITableView中制作部分时遇到问题。我查看了UILocalizedIndexedCollation的文档以及此示例代码项目:

https://developer.apple.com/library/ios/#samplecode/TableViewSuite/Listings/3_SimpleIndexedTableView_Classes_RootViewController_m.html#//apple_ref/doc/uid/DTS40007318-3_SimpleIndexedTableView_Classes_RootViewController_m-DontLinkElementID_18

我在下面的内容基本上是来自示例项目的直接复制/粘贴。但是,示例项目使用自定义对象(TimeZoneWrapper.h),然后根据对象的实例变量(TimeZoneWrapper.localeName)将对象放在正确的部分中。但是,我没有使用自定义对象。我只使用了一堆常规的NSStrings。所以我的问题是我应该将NSString上的哪个方法传递给@selector()来比较并将字符串放在正确的section数组中?

目前,我正在调用NSString的复制方法作为临时黑客来使事情正常工作(它确实如此),但我不确定这是否正确。一点点解释将非常感激!

- (void)configureSections {

// Get the current collation and keep a reference to it.
self.collation = [UILocalizedIndexedCollation currentCollation];

NSInteger index, sectionTitlesCount = [[collation sectionTitles] count]; // sectionTitles are A, B, C, etc.

NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

// Set up the sections array: elements are mutable arrays that will contain the locations for that section.
for (index = 0; index < sectionTitlesCount; index++) {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [newSectionsArray addObject:array];
}

// Segregate the loctions into the appropriate arrays.
for (NSString *location in locationList) {

    // Ask the collation which section number the location belongs in, based on its locale name.
    NSInteger sectionNumber = [collation sectionForObject:location collationStringSelector:@selector(/* what do I put here? */)];

    // Get the array for the section.
    NSMutableArray *sectionLocations = [newSectionsArray objectAtIndex:sectionNumber];

    //  Add the location to the section.
    [sectionLocations addObject:location];
}

// Now that all the data's in place, each section array needs to be sorted.
for (index = 0; index < sectionTitlesCount; index++) {

    NSMutableArray *locationsArrayForSection = [newSectionsArray objectAtIndex:index];

    // If the table view or its contents were editable, you would make a mutable copy here.
    NSArray *sortedLocationsArrayForSection = [collation sortedArrayFromArray:locationsArrayForSection collationStringSelector:@selector(/* what do I put here */)];

    // Replace the existing array with the sorted array.
    [newSectionsArray replaceObjectAtIndex:index withObject:sortedLocationsArrayForSection];
}

self.sectionsArray = newSectionsArray;
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您应该使用@selector(self)

使用@selector(copy)会导致项目内存泄漏