UILocalizedIndexedCollation的sectionForObject:(id)object collationStringSelector:(SEL)selector方法中选择器的作用是什么 [英] What is the role of selector in UILocalizedIndexedCollation's sectionForObject:(id)object collationStringSelector:(SEL)selector method
问题描述
I am a beginner of iOS development and while going through this document (iOS Developer Guide about configuring a TableView with Indexed List) I came across this:
// Listing 4.7
for (State *theState in statesTemp) {
NSInteger sect = [theCollation sectionForObject:theState collationStringSelector:@selector(name)];
theState.sectionNumber = sect;
}
我无法确定选择器(@selector(name)
)及其用途,也无法找到在选择器中传递名称的方法,即name
.我搜索了一些示例以找到更好的解释,并遇到了此示例.
I could not figure out the selector (@selector(name)
) and its purpose, nor could I find the method with the name passed in the selector i.e. name
. I googled for examples to find a better explanation, and came across this example.
在代码清单中,有一条语句是方法调用:
In the code listing, there is a statement which is a method call:
self.tableData = [self partitionObjects:objects collationStringSelector:@selector(title)];
现在选择器称为title
.我找不到更好的解释,我的问题是此选择器的目的是什么以及该选择器引用的方法,它应该做什么并返回.
now the selector is called title
. I have not been able to find a better explanation, and my question is what is the purpose of this selector and the method referred by this selector, and what should it do and return.
推荐答案
一般
使用@selector(title:)
定义将调用的方法.
In general
With the @selector(title:)
you define which method will be called.
在我的示例中,它将调用
in my example it will call
- (void) title:(id)someObject {}
最后请小心用分号!如果最后使用分号,则您的方法将具有上面类似我的参数.
Be carefull with the semicolon at the end! If you have a semicolon at the end you method will have parameters like mine above.
您的代码仅声明了@selector(title)
,并且将调用不带参数的方法标题,如下所示:
Your code states just @selector(title)
and will call a method title without a parameter like this:
- (void)title {}
特定于UILocalizedIndexCollation
文档状态:
Specific to UILocalizedIndexCollation
The docs state:
选择器
选择器,用于标识返回标识的方法 排序规则中使用的对象的字符串.该方法不应该 参数并返回一个NSString对象.例如,这可能是 对象的名称属性.
selector
A selector that identifies a method returning an identifying string for object that is used in collation. The method should take no arguments and return an NSString object. For example, this could be a name property on the object.
所以我建议您像这样实现它
So i would suggest you implement it like this
self.tableData = [self partitionObjects:objects collationStringSelector:@selector(title)];
...
- (NSString *)title {
NSString *title;
// some code to fill title with an identifier for your object
return title;
}
这篇关于UILocalizedIndexedCollation的sectionForObject:(id)object collationStringSelector:(SEL)selector方法中选择器的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!