在UILocalizedIndexedCollat​​ion的sectionForObject中select器的作用是什么:(id)object collat​​ionStringSelector:(SEL)selector method

我是iOS开发的初学者,在阅读本文档(iOS开发人员指南关于configuration带有索引列表的TableView)时遇到了这个问题:

// Listing 4.7 for (State *theState in statesTemp) { NSInteger sect = [theCollation sectionForObject:theState collationStringSelector:@selector(name)]; theState.sectionNumber = sect; } 

我无法弄清select器(@select器( @selector(name) )及其用途,也不能find名称传递给select器name 。 我search了一些例子来find更好的解释,并且遇到了这个例子。

在代码清单中,有一个声明是一个方法调用:

 self.tableData = [self partitionObjects:objects collationStringSelector:@selector(title)]; 

现在select器被称为title 。 我一直没有find更好的解释,我的问题是这个select器的目的是什么,这个select器引用的方法是什么,它应该做什么和返回。

一般来说

用@select器@selector(title:)定义哪个方法将被调用。

在我的例子中,它会调用

 - (void) title:(id)someObject {} 

最后用分号小心! 如果你在最后有一个分号,你的方法将会有像上面这样的参数。

你的代码状态只是@select器@selector(title)并会调用一个方法标题没有这样的参数:

 - (void)title {} 

特定于UILocalizedIndexCollat​​ion

文档状态:

select
标识一个方法的select器,它返回在整理中使用的对象的标识string。 该方法应该不带参数并返回一个NSString对象。 例如,这可能是对象的名称属性。

所以我build议你像这样实施它

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

尝试用selfreplacetitle

 self.tableData = [self partitionObjects:objects collationStringSelector:@selector(self)]; 

为我工作