2013-12-19 12 views
1

我已經構建了一個很好的TableView,其中包含部分,搜索和右側的字母條。如何在包含章節和搜索的UITableView中編寫PrepareForSeque

我測試了它,一切都很順利,直到我注意到「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:邏輯是這樣的:

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

您執行SEGUE時需要做出的邏輯匹配填充單元格時的單元格,從單元格的正常工作。看來,你做正確的時候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。 –