2010-06-08 2 views
1
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 

    if(tempArray != nil){ 
     for (int i = 0; i < [tempArray count]; i++) 
     { 
      if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]]) 
     // if([sectionInfo indexTitle] == [tempArray objectAtIndex:i]) 
      { 
       NSLog(@"found"); 
       break; 
      } else 
       { 
        NSLog(@"Not found %@", [sectionInfo indexTitle]); 
        [tempArray addObject:[sectionInfo indexTitle]]; 
        NSLog(@"array %@", tempArray); 
        return [tempArray objectAtIndex:i]; 
       } 
     } 
    } 
} 

if 문의 문자열 비교는 절대로 true로 해석되지 않습니다. 샘플 데이터에는 테스트 목적으로 두 개의 중복 인스턴스가 있습니다. 내가 틀린 것으로 생각 되긴하지만 주석 처리 된 선은 대체 부분이며, 섹션을 tempArray의 문자열과 비교하려고 시도합니다.Objective-c : 두 개의 문자열이 올바르게 작동하지 않음을 비교합니다.

내가 뭘 잘못하고 있니?

또한 모든 데이터는 대문자이므로 대문자와 소문자의 비교는 아닙니다.

편집 :

NSLogs :

2010-06-08 11:25:03.276 XYZApplication[5287:207] Not found Z 
2010-06-08 11:25:03.277 XYZApplication[5287:207] section: 10 
2010-06-08 11:25:03.279 XYZApplication[5287:207] [sectionInfo indexTitle] Z 
2010-06-08 11:25:03.284 XYZApplication[5287:207] [sectionInfo objects] (
    Z 
) 
2010-06-08 11:25:03.284 XYZApplication[5287:207] array (
    "" 
) 
2010-06-08 11:25:03.285 XYZApplication[5287:207] Not found A 
2010-06-08 11:25:03.285 XYZApplication[5287:207] section: 0 
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo indexTitle] A 
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo objects] (
    "Andy the Clown" 
) 
2010-06-08 11:25:03.287 XYZApplication[5287:207] array (
    "", 
    Z 
) 
2010-06-08 11:25:03.288 XYZApplication[5287:207] Not found B 
2010-06-08 11:25:03.289 XYZApplication[5287:207] section: 1 
2010-06-08 11:25:03.289 XYZApplication[5287:207] [sectionInfo indexTitle] B 
2010-06-08 11:25:03.290 XYZApplication[5287:207] [sectionInfo objects] (
    "Baseball Annie" 
) 
2010-06-08 11:25:03.291 XYZApplication[5287:207] array (
    "", 
    Z, 
    A 
) 
2010-06-08 11:25:03.291 XYZApplication[5287:207] Not found B 
2010-06-08 11:25:03.292 XYZApplication[5287:207] section: 2 
2010-06-08 11:25:03.292 XYZApplication[5287:207] [sectionInfo indexTitle] B 
2010-06-08 11:25:03.293 XYZApplication[5287:207] [sectionInfo objects] (
    "Baseball Bugs" 
) 
2010-06-08 11:25:03.293 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B 
) 
2010-06-08 11:25:03.294 XYZApplication[5287:207] Not found D 
2010-06-08 11:25:03.295 XYZApplication[5287:207] section: 3 
2010-06-08 11:25:03.295 XYZApplication[5287:207] [sectionInfo indexTitle] D 
2010-06-08 11:25:03.296 XYZApplication[5287:207] [sectionInfo objects] (
    "" 
) 
2010-06-08 11:25:03.296 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B 
) 
2010-06-08 11:25:03.296 XYZApplication[5287:207] Not found E 
2010-06-08 11:25:03.304 XYZApplication[5287:207] section: 4 
2010-06-08 11:25:03.305 XYZApplication[5287:207] [sectionInfo indexTitle] E 
2010-06-08 11:25:03.306 XYZApplication[5287:207] [sectionInfo objects] (
    E 
) 
2010-06-08 11:25:03.306 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D 
) 
2010-06-08 11:25:03.307 XYZApplication[5287:207] Not found F 
2010-06-08 11:25:03.307 XYZApplication[5287:207] section: 5 
2010-06-08 11:25:03.308 XYZApplication[5287:207] [sectionInfo indexTitle] F 
2010-06-08 11:25:03.310 XYZApplication[5287:207] [sectionInfo objects] (
    Frank 
) 
2010-06-08 11:25:03.310 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D, 
    E 
) 
2010-06-08 11:25:03.311 XYZApplication[5287:207] Not found H 
2010-06-08 11:25:03.311 XYZApplication[5287:207] section: 6 
2010-06-08 11:25:03.312 XYZApplication[5287:207] [sectionInfo indexTitle] H 
2010-06-08 11:25:03.313 XYZApplication[5287:207] [sectionInfo objects] (
    H 
) 
2010-06-08 11:25:03.313 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D, 
    E, 
    F 
) 
2010-06-08 11:25:03.314 XYZApplication[5287:207] Not found J 
2010-06-08 11:25:03.315 XYZApplication[5287:207] section: 7 
2010-06-08 11:25:03.315 XYZApplication[5287:207] [sectionInfo indexTitle] J 
2010-06-08 11:25:03.316 XYZApplication[5287:207] [sectionInfo objects] (
    J 
) 
2010-06-08 11:25:03.316 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D, 
    E, 
    F, 
    H 
) 
2010-06-08 11:25:03.317 XYZApplication[5287:207] Not found J 
2010-06-08 11:25:03.317 XYZApplication[5287:207] section: 8 
2010-06-08 11:25:03.329 XYZApplication[5287:207] [sectionInfo indexTitle] J 
2010-06-08 11:25:03.330 XYZApplication[5287:207] [sectionInfo objects] (
    Joe 
) 
2010-06-08 11:25:03.330 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D, 
    E, 
    F, 
    H, 
    J 
) 
2010-06-08 11:25:03.335 XYZApplication[5287:207] Not found L 
2010-06-08 11:25:03.336 XYZApplication[5287:207] section: 9 
2010-06-08 11:25:03.336 XYZApplication[5287:207] [sectionInfo indexTitle] L 
2010-06-08 11:25:03.337 XYZApplication[5287:207] [sectionInfo objects] (
    L 
) 
2010-06-08 11:25:03.337 XYZApplication[5287:207] array (
    "", 
    Z, 
    A, 
    B, 
    B, 
    D, 
    E, 
    F, 
    H, 
    J, 
    J 
) 
+0

당신이 문자열의 복사본을 인쇄 할 NSLog를 사용하여 수 있을까요? 추가 공백, 다른 인코딩 등 많은 이유가있을 수 있습니다. – Emil

+0

루프가 첫 번째 반복을 지나치지 않습니다. 왜 루프인가? – drawnonward

답변

1

tempArray의 첫 번째 요소는 항상 빈 문자열로 보입니다. 로그에서 [sectionInfo indexTitle]은 빈 문자열이 아닙니다.

처음 뭔가를 찾지 못했을 때 방법을 종료 했으므로 진정한 비교를 얻지 못할 것입니다.

또한 발견하지 못한 모든 반복마다 tempArray에 [sectionInfo indexTitle]을 찾을 수 없습니다. 그게 네가 정말로 원하는거야?

또한 일치하는 항목을 찾은 경우 아무 것도 반환하지 않습니다. 메서드가 항상 값을 반환하지 않는다고 말하는 컴파일러 경고가 있어야합니다.

나는 가지 여기 추측하고있어하지만 난 당신이 뭔가 할 생각 : 내가 마지막으로 당신이 뭘 하려는지 이해했다고 생각

: 아래의 코멘트

for (int i = 0; i < [tempArray count]; i++) // NB [nil count] returns 0 so your test for a nil tempArray is unnecessary 
    { 
     if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]]) 
     { 
      NSLog(@"found"); 
      return [tempArray objectAtIndex:i]; 
     } 
     else 
     { 
      NSLog(@"Not found %@", [sectionInfo indexTitle]); 
      NSLog(@"array %@", tempArray); 
     } 
    } 
    [tempArray addObject:[sectionInfo indexTitle]]; 
    return ???? // I don't know what you want returned in this case: 

편집을 :

이미 전달하려는 섹션 제목이 이미있는 경우 섹션에 제목이 없도록 nil을 반환하고 싶습니다.이 경우, 코드는 다음과 같아야합니다

for (int i = 0; i < [tempArray count]; i++) // NB [nil count] returns 0 so your test for a nil tempArray is unnecessary 
    { 
     if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]]) 
     { 
      NSLog(@"found"); 
      return nil; 
     } 
     else 
     { 
      NSLog(@"Not found %@", [sectionInfo indexTitle]); 
      NSLog(@"array %@", tempArray); 
     } 
    } 
    [tempArray addObject:[sectionInfo indexTitle]]; 
    return [sectionInfo indexTitle]; 

그러나

대신 NSMutableSet를 사용할 수 있습니다

if ([tempSet containsObject: [sectionInfo indexTitle]]) 
{ 
    return nil; 
} 
else 
{ 
    [tempSet addObject: [sectionInfo indexTitle]]; 
    return [sectionInfo indexTitle]; 
} 
+0

이것은 100 %가 아니지만 지금까지 가장 잘 작동하는 것 같습니다. 내가 뭘 하려는지 내 tableview 알파벳순으로 그룹화됩니다. 데이터는 SQLite DB에서 제공되는, 그 차이가 있습니다.이 날 적절한 배열을 "tempArray"중복없이 있지만 tableview 올바르게 표시되지 않습니다 .AB 및 J 표시하고 각 중복 된 항목이 있습니다. 내가 groupin 무엇을 조사해야합니다 그 첫 번째 글자로 세포를 채우는거야? –

+0

두 코드 모두 동일한 결과를 산출합니다. 각 셀에 섹션 헤더를 추가하는 것 같습니다. 세포를 분류하는 방법을 알아 내려고하는 참고서가 있습니까? –

+0

이 메서드를 호출 할 시점까지 테이블 뷰의 구조가 이미 결정되었습니다. 귀하의 그룹핑 로직은 -numberOfSectionsInTableView : 및 -tableView : numberOfRowsInSection의 구현에 구현되어야합니다. 다음 링크는 이러한 사용법을 알려줍니다 : http://developer.apple.com/iphone/library/documentation/UserExperience/ 개념적/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html # // apple_ref/doc/uid/TP40007451-CH6-SW5 – JeremyP

1

당신에게 tempArray의 개체가 정말 NSString 있는지 위치 :

   NSLog(@"Not found %@", [sectionInfo indexTitle]); 
       NSLog(@"section: %d", section); 
       NSLog(@"[sectionInfo indexTitle] %@", [sectionInfo indexTitle]); 
       NSLog(@"[sectionInfo objects] %@", [[sectionInfo objects] valueForKey:@"name"]); 
       NSLog(@"array %@", tempArray); 

나에게 결과를 제공? isEqualToString은 다른 유형 인 경우 false를 반환합니다.

또한 문자열에 공백 문자를 사용할 수 있습니까?

+0

tempArray는 @ ""빈 문자열로 초기화 된 NSMutableArray입니다. 배열에는 하나의 항목이 있습니다. tempArray는 이미 추가 된 머리글의 레코드로만 사용됩니다. 추가 된 유일한 항목은 단일 문자 "섹션 인덱스"입니다. –

2

첫 번째 요소가 일치하지 않으면 else 분기의 return이 검색을 중단합니다. 첫 번째 요소가 일치하면 'then'브랜치가 루프에서 빠져 나옵니다 (그리고 그 케이스에 대해 반환 값을 제공하는 코드가 더 많습니다).

return은 'then'분기의 break을 대체해야합니까?

또한 원하는 문자열을 [sectionInfo indexTitle]으로 지정 했으므로 왜 검색할까요? 목록에 있는지 만 확인하면됩니까? 루프 대신 다음과 같은 내용이있을 수 있습니다.

if ([tempArray containsObject: [sectionInfo indexTitle]]) { 
    return [sectionInfo indexTitle]; 
} else { 
    return @"<invalid>"; // or whatever 
} 
+0

무한 루프가 발생하는 것 같습니다. 논리적으로, 나는 왜 확신 할 수 없다. –

+0

@Mr : 전체 배열을 검색하려면 첫 번째 배열이 일치하지 않으면 중단하지 않아야합니다. 수정 된 루프가 "무한"이라고 확신합니까? 그것은 각 호출에 대해 더 많은 "Not found"로그를 "분출"합니다. –

+1

개체 존재 여부 만 테스트 할 때'containsObject :'보다는'indexOfObject :'를 더 선호합니다. – Nefrubyr

관련 문제