2012-03-15 38 views
0

的代碼工作正常,但是當我對一個特定的字母添加一個以上的值,它就會被重複。如果該字母表有單個值,則數據顯示正確 - 我哪裏錯了?排序值不正確歌廳顯示的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

回答

0

是的,你正在使用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 <[tempArray計數] -1;我++){ \t \t \t炭字母表= [[tempArray objectAtIndex:ⅰ] characterAtIndex:0]; \t \t \t的NSString * UNICHAR = [NSString的stringWithFormat:@ 「%C」,字母]; \t \t \t的NSArray *重複= [Frstname filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@ 「%C」,字母]]; \t \t \t //如果: \t \t \t如果([重複計數] == 0){ \t \t \t \t [Frstname ADDOBJECT:UNICHAR]([Frstname containsObject UNICHAR]!); – 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

0

你有沒有調試你的代碼? 你正在因爲這些線路

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

0

假設你正試圖從一個姓插入所有不重複的字符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