2013-12-19 4 views
1

오른쪽에 섹션, 검색 및 알파벳 스트립이 포함 된 멋진 TableView를 만들었습니다.섹션과 검색이있는 PrepareForSeque를 UITableView에 코딩하는 방법

"A"가 제대로 작동하지 않는 것을 알아 채기 전까지는 테스트를 거쳤습니다. 이유를 발견했습니다. PrepareForSegue에서 행에서 찾은 객체를 전달했지만 섹션에 관심을 기울이지 않았습니다.

스택 오버플로를 검색하여 수행하려고하는 것과 같은 것을 발견했지만 didSelectRowAtIndexPath에서 코딩 중이며, 그렇지 않은 경우를 대비합니다. 그리고 코드를 작동시킬 수 없었습니다.

첫 번째 질문은 두 가지 방법 사이의 관계는 무엇입니까? 나는 스토리 보드를 사용하고 있기 때문에 나는 그 섹슈얼을 고수해야한다고 생각한다. 그렇다면 어떤 직원 객체를 통과 시킬지 어떻게 알 수 있습니까?

도움을 주시면 감사하겠습니다.

브라이언

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     employee *dtlEmployee = [_employees objectAtIndex:indexPath.row]; 
     self.detailViewController.detailItem = dtlEmployee; 
    } 
    else 
    { 
//  employee *dtlEmployee; 
//   
//  int row = indexPath.row; 
//  int section = indexPath.section; 
//   
//  NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
//   
//  [self.tableView deselectRowAtIndexPath:newIndexPath animated:NO]; 
//  UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath]; 
// 
//  dtlEmployee = [_employees objectAtIndex:newIndexPath]; 

    } 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 

     employee *dtlEmployee; 

//  int row = [[self.tableView] indexPath.row]; 
//  int section = [self.tableView indexPath.section]; 
//  NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
//  dtlEmployee = [_employees objectAtIndex:newIndexPath]; 
//  NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow]; 
//  NSUInteger selectedRow = selectedRowIndexPath.row; 
//  NSUInteger selectedSection = selectedRowIndexPath.section; 
//  NSArray *tmpArray = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow]]; 
//  dtlEmployee = [tmpArray objectAtIndex:selectedRowIndexPath.row]; 
//  dtlEmployee = [tmpArray objectAtIndex:indexPath.row]; 
//   
//  dtlEmployee = [_employees objectAtIndex:selectedRowIndexPath.] 
//   
     if (isSearching) 
     { 
      dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row]; 
     } 
     else 
     { 
     dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row]; 
     } 

     [[segue destinationViewController] setDetailItem:dtlEmployee]; 

    } 
} 

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [self.tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
     //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    if (isSearching) 
    {employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row]; 
     cell.textLabel.text = thisEmployee.fullName;} 
    else 
    {employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 
     cell.textLabel.text = thisEmployee.fullName;} 

    return cell; 
    } 
+0

당신은 당신의 cellForRowAtIndexPath 방법을 포함시겠습니까? – Gavin

+0

네, –

답변

1
tableView:cellForRowAtIndexPath:에 적합한 직원을 얻기위한 당신의 논리는 다음과 같습니다

:

if (isSearching) 
{ 
    employee *thisEmployee = [self.filteredEmployees objectAtIndex:indexPath.row]; 
    cell.textLabel.text = thisEmployee.fullName; 
} 
else 
{ 
    employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 
    cell.textLabel.text = thisEmployee.fullName; 
} 

그러나 prepareForSegue:sender:의 논리는 다음과 같습니다 다음 SEGUE을 수행 할 때

if (isSearching) 
{ 
    dtlEmployee = [self.filteredEmployees objectAtIndex:self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row]; 
} 
else 
{ 
    dtlEmployee = [_employees objectAtIndex:[self.tableView indexPathForSelectedRow].row]; 
} 

당신은 논리를 만들 필요가 그것의 소리에서 올바르게 작동하는 세포를 채울 때 하나를 일치 시키십시오. isSearching에 해당하는 경우 당신은 제대로하고있는 것으로 보인다, 그래서 그것은 사실이 아니다 때, 다음 줄을 사용해야합니다 :

employee *thisEmployee = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex: [self.tableView indexPathForSelectedRow].section]] objectAtIndex: [self.tableView indexPathForSelectedRow].row]; 
+0

잘 작동합니다. 나는 6 줄의 코드를 사용하는 회선 방식을 사용했다. –

+0

// UITableViewCell * cell = (UITableViewCell *) sender; // NSIndexPath * indexPath = [self.tableView indexPathForCell : cell]; // NSInteger totalRows = indexPath.row; // 현재 섹션 행 값 // // 직원이있는 행 찾기 // (int n = 0; n

+0

그러나 당신의 방법은 더 간결하고 논리적입니다. 고마워요! –

3

당신의 SEGUE가 테이블 뷰 셀에 연결되어 있는지 가정은 prepareForSegue:에 보낸 사람이 실제로 세포이다. (로깅 또는 디버거를 사용하십시오.)

즉, 셀에서 일치하는 인덱스 경로를 테이블 뷰에 요청하고 경로에서 개체를 가져 오는 것과 동일한 논리를 tableView:cellForRowAtIndexPath:에 사용할 수 있습니다.

+0

위에 추가했습니다. 테이블 셀을 반환하는지 확인 했으므로 tableview : cellForRowAtIndexPath의 로직을 사용하여 작동하도록하려고 시도 할 것입니다. –

관련 문제