简体   繁体   中英

how to set # in alphabetical index headers UITable for numerical values in iphone

I have implemented alphabetical section headers using the link http://www.devx.com/wireless/Article/43374

Its good to use.

i used this logic to get first charecter as indexes

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

}

But in case of contacts it returns 1,2,3...

在此处输入图片说明

But need to display # instead of all numaerical values headers

if (alphabet >= '0' && alphabet <= '9') alphabet = '#';

Use NSNumberFormatter to check if NSString is numeric or not

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM