0

我已经使用链接http://www.devx.com/wireless/Article/43374实现了按字母顺序排列的部分标题

它很好用。

我用这个逻辑来获取第一个字符作为索引

  // To implement indexes and section headers
  contactsArray = [[NSMutableArray alloc] init];
  for(int i=0; i< tableViewArray.count ; i++)
  {
        Contact *contact = [tableViewArray objectAtIndex:i];
        [contactsArray addObject:contact.name];
  }



 contactIndex = [[NSMutableArray alloc] init];
  for (int i=0; i<[contactsArray count]; i++){
        //---get the first char of each contactName---
        char alphabet = [[[contactsArray objectAtIndex:i] uppercaseString] characterAtIndex:0];

    NSString *firstChar = [NSString stringWithFormat:@"%C", alphabet];

    //---add each letter to the index array---
    if (![contactIndex containsObject:firstChar])
    {            
          [contactIndex addObject:firstChar];
    }        

}

但如果是联系人,它会返回1,2,3...

在此处输入图像描述

But need to display # instead of all numaerical values headers

4

2 回答 2

0
if (alphabet >= '0' && alphabet <= '9') alphabet = '#';
于 2012-11-09T10:56:33.280 回答
0

使用NSNumberFormatter检查 NSString 是否为数字

// To implement indexes and section headers
  contactsArray = [[NSMutableArray alloc] init];
  for(int i=0; i< tableViewArray.count ; i++)
  {
        Contact *contact = [tableViewArray objectAtIndex:i];
        [contactsArray addObject:contact.name];
  }



 contactIndex = [[NSMutableArray alloc] init];
  for (int i=0; i<[contactsArray count]; i++){
        //---get the first char of each contactName---
        char alphabet = [[[contactsArray objectAtIndex:i] uppercaseString] characterAtIndex:0];

    NSString *firstChar = [NSString stringWithFormat:@"%C", alphabet];

   NSNumberFormatter *testFormatter = [[NSNumberFormatter alloc] init];

  if(nil == [testFormatter numberFromString: firstChar]){

      firstChar = @"#";
  }

    //---add each letter to the index array---
    if (![contactIndex containsObject:firstChar])
    {            
          [contactIndex addObject:firstChar];
    }       
}
于 2012-11-09T11:04:52.107 回答