排序值不正确歌厅显示的tableview在iphone

问题描述:

的代码工作正常,但是当我对一个特定的字母添加一个以上的值,它就会被重复。如果该字母表有单个值,则数据显示正确 - 我哪里错了?排序值不正确歌厅显示的tableview在iphone

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    PeopleModal *dislist = [[PeopleModal alloc]init]; 
    [dislist getAll]; 

    personarray = [[NSMutableArray alloc]init]; 
    [personarray addObjectsFromArray:dislist.Peoplelistarray]; 
    //short the personarray value: 
    NSSortDescriptor *asortDescriptor; 
    asortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"FirstName" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

    NSArray *sortDescriptors=[NSArray arrayWithObject:asortDescriptor]; 
    [self.personarray sortUsingDescriptors:sortDescriptors]; 

    //---create the index--- 
    Frstname = [[NSMutableArray alloc] init]; 

     //check 
    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
    for (NSDictionary *row in personarray) { 
     [tempArray addObject:[row valueForKey:@"FirstName"]]; 

     for (int i=0; i<[tempArray count]-1; i++){ 
      char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0]; 
      NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; 
      if (![Frstname containsObject:uniChar]){ 
       [Frstname addObject:uniChar];   
      } 
     } 

    } 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 

    return [Frstname count]; 

} 

//---set the title for each section--- 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    return [Frstname objectAtIndex:section]; 

} 

//---set the index for the table--- 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 

     return Frstname; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if (isSearchOn) { 
     return [searchResult count]; 
    } else{ 
//return personarray.count; 
    //---get the letter in each section; e.g., A, B, C, etc.--- 
    NSString *alphabet = [Frstname objectAtIndex:section]; 
    //---get all FirstName beginning with the letter---beginswith 
    NSPredicate *predicate = 
    [NSPredicate predicateWithFormat:@"SELF.FirstName beginswith[c] %@", alphabet]; 
    NSArray *Names = [personarray filteredArrayUsingPredicate:predicate]; 
    //---return the number of Names beginning with the letter--- 
    return [Names count]; 
    } 

} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    if (isSearchOn) { 
     NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
     cell.textLabel.text = cellValue; 
    } 
    else { 

    //---get the letter in the current section--- 
    NSString* alphabet = [Frstname objectAtIndex:[indexPath section]]; 
    //---get all states beginning with the letter--- 
    NSPredicate *predicate = 
     [NSPredicate predicateWithFormat:@"SELF.FirstName beginswith[c] %@", alphabet]; 
    //[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 
    //NSArray *Names = [[personarray valueForKey:@"FirstName"] filteredArrayUsingPredicate:predicate]; 
     NSArray *Names = [personarray filteredArrayUsingPredicate:predicate]; 
    //NSArray *lastNames = [[personarray valueForKey:@"LastName"] filteredArrayUsingPredicate:predicate]; 
    if ([Names count]>0) { 
     //---extract the relevant firstname from the Names object--- 


     PeopleModal *locallist = [Names objectAtIndex:indexPath.row]; 

     cell.textLabel.text = locallist.FirstName; 
     cell.detailTextLabel.text=locallist.LastName;   
    } 

    } 


    return cell; 





} 
+0

你可以编辑你的答案,并保留代码块的第一行吗?这很难阅读。 – Madhu 2012-03-15 09:14:17

是的,你正在使用containsObject,这将永诺返回false,因为这个对象的id是一个新的NSString让你的代码在阵列添加相同的NSString值。试试这样的:

NSArray *duplicates = [Frstname filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%C", alphabet]]; 
if ([duplicates count] == 0) { 
    [Frstname addObject:uniChar] 
} 
+0

NSMutableArray * tempArray = [[NSMutableArray alloc] init]; \t for(NSDictionary * row in personarray){ \t \t [tempArray addObject:[row valueForKey:@“FirstName”]]; \t \t \t 为\t(INT I = 0; I Rocky 2012-03-15 09:31:40

+0

我根据UI尝试得到错误这个 'NSInvalidArgumentException' 的,理由是: '无法解析格式字符串 “%C”' – Rocky 2012-03-15 09:32:14

+0

抱歉..这样'NSPredicate *谓词= [NSPredicate predicateWithFormat:@“SELF beginswith [C]” %C”,字母]' – spacebiker 2012-03-15 09:59:55

你有没有调试你的代码? 你正在因为这些线路

for (int i=0; i<[tempArray count]-1; i++){ 
     char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0]; 
     NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; 
     if (![Frstname containsObject:uniChar]){ 
      [Frstname addObject:uniChar]; 

     } 

在这里你正在运行的重复值在侧环路另一个用于loop.so这将被调用为每一个迭代。 代替for循环使用

char alphabet = [[[row valueForKey:@"FirstName"] characterAtIndex:0]; 
    NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; 
    if (![Frstname containsObject:uniChar]){ 
     [Frstname addObject:uniChar]; 

我希望你正在写的代码,以获得独特的前几个字符...

+0

嗨我尝试你的代码,但同样的问题让我 – Rocky 2012-03-15 09:42:23

+0

你有没有删除内循环? – 2012-03-15 10:19:18

+0

是的,我删除内圈forloop – Rocky 2012-03-15 10:21:37

假设你正试图从一个姓插入所有不重复的字符Frstname阵列,改变该:

//check 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
for (NSDictionary *row in personarray) { 
    [tempArray addObject:[row valueForKey:@"FirstName"]]; 

    for (int i=0; i<[tempArray count]-1; i++){ 
     char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0]; 
     NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; 
     if (![Frstname containsObject:uniChar]){ 
      [Frstname addObject:uniChar]; 
     } 
    } 

} 

此:

//check 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
for (NSDictionary *row in personarray) { 
    [tempArray addObject:[row valueForKey:@"FirstName"]]; 

    for (int i=0; i<[tempArray count]-1; i++){ 
     char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0]; 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] '%c'", alphabet]; 
     NSArray *duplicates = [Frstname filteredArrayUsingPredicate:predicate]; 
     if ([duplicates count] == 0) { 
      [Frstname addObject:uniChar]; 
     } 
    } 
} 
+0

的字典中,我有两个值firstname和lastname,我希望根据所有firstname字符在表上显示,如果我有打印amit名称,如果我hav eb然后像这样打印像这样就像iPhone手机directry显示那样我想在桌面视图上显示所有名字 – Rocky 2012-03-15 10:55:08

+0

蚂蚁这个ans是崩溃代表unichar使用什么 – Rocky 2012-03-15 10:55:48

+0

你可以用'uniChar'代替'alphabet'的'uniChar'如果你想它作为字符串只是替换为'[NSString stringWithFormat:@“%C”,英文字母]'unichar'' – spacebiker 2012-03-15 11:05:55