0

我有一个NSMutableArray *objectsArray;包含字典,每个字典NSDictionary都有一个"Name"以对象名称作为值的字符串(不足为奇)。UITableView已按NSSortDescriptor名称按升序排序。

现在我需要使用名称的第一个字母section来访问 TableView ,但我找不到所需的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    /*Return: How many letters from a-z are represented
    as the first letter of the objects name? 
    I.e. If there is no object in the dictionary with name starting with letter B, 
    the number of sections are now 25 if all the other letters are included.*/
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   //Return: How many objects are starting with each letter

所以我需要知道如何返回正确的部分和行数?

附言。我还需要做些什么才能使其正常工作,例如在单元格创建过程中cellForRowAtIndexPath或在将 传递objectsArraysegueDetailViewController

有关应用程序结构的其他信息:

objectsArray 来自带有字典的 plist 数组。使用自定义原型单元格填充表格视图。带有故事板的 Xcode 4.2。适用于 iPhone 5.1 的应用程序

4

1 回答 1

1

NSMutableSet在排序之前创建一个。让您的比较器在您的集合中存储它在排序时遇到的每个名称的第一个字母。那么部分的数量就是你的集合的大小。这样,您只需支付很少的成本,因为您的比较器无论如何都必须遍历数组。

于 2012-08-23T17:21:59.960 回答