如何根据姓氏创建索引表视图

时间:2022-09-14 09:27:46

If you could find a tutorial for an indexed table view based on last name like the "Contacts" app, that would be great. I've search all around but can't find one on last names.

如果你能找到一个基于姓氏的索引表视图的教程,比如“联系人”应用程序,那就太棒了。我四处搜索,但找不到姓氏。

1 个解决方案

#1


0  

I guess you store your contacts using core data. You probably have a entity "Person" with attributes first name, last name, address etc.

我猜你用核心数据存储你的联系人。您可能有一个实体“Person”,其中包含属性的名字,姓氏,地址等。

You should make a subclass of NSManagedObject specifically for the entity "Person". In this class, you add a method that returns only the first letter of the last name. If you have a NSFetchedResultsController you can apply the path name for the sections. There you just write the name of exactly that method that returns the first letter. The table view should then be sectioned by the last name's first letter like in contacts.

您应该专门为实体“Person”创建NSManagedObject的子类。在此类中,您添加一个只返回姓氏的第一个字母的方法。如果您有NSFetchedResultsController,则可以应用这些节的路径名。在那里你只需要写一个返回第一个字母的方法的名称。然后,表格视图应按姓氏的第一个字母(如联系人)划分。

Now, you should implement the method

现在,您应该实现该方法

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

You return a array that contains all the index titles which will appear on the right. Let's say if your contacts are "Steve Jobs" and "Jony Ive", there would be two sections and two index titles "I" and "J". You don't have to put all letters of the whole alphabet in the array, just those that you really need.

您将返回一个数组,其中包含将出现在右侧的所有索引标题。假设您的联系人是“史蒂夫·乔布斯”和“Jony Ive”,那么会有两个部分和两个索引标题“I”和“J”。您不必将整个字母的所有字母都放在数组中,只需要那些您真正需要的字母。

#1


0  

I guess you store your contacts using core data. You probably have a entity "Person" with attributes first name, last name, address etc.

我猜你用核心数据存储你的联系人。您可能有一个实体“Person”,其中包含属性的名字,姓氏,地址等。

You should make a subclass of NSManagedObject specifically for the entity "Person". In this class, you add a method that returns only the first letter of the last name. If you have a NSFetchedResultsController you can apply the path name for the sections. There you just write the name of exactly that method that returns the first letter. The table view should then be sectioned by the last name's first letter like in contacts.

您应该专门为实体“Person”创建NSManagedObject的子类。在此类中,您添加一个只返回姓氏的第一个字母的方法。如果您有NSFetchedResultsController,则可以应用这些节的路径名。在那里你只需要写一个返回第一个字母的方法的名称。然后,表格视图应按姓氏的第一个字母(如联系人)划分。

Now, you should implement the method

现在,您应该实现该方法

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

You return a array that contains all the index titles which will appear on the right. Let's say if your contacts are "Steve Jobs" and "Jony Ive", there would be two sections and two index titles "I" and "J". You don't have to put all letters of the whole alphabet in the array, just those that you really need.

您将返回一个数组,其中包含将出现在右侧的所有索引标题。假设您的联系人是“史蒂夫·乔布斯”和“Jony Ive”,那么会有两个部分和两个索引标题“I”和“J”。您不必将整个字母的所有字母都放在数组中,只需要那些您真正需要的字母。