0

我在我的主視圖上有一個表視圖,在我的詳細信息視圖上有一個textField。該應用程序很簡單。用一些信息添加一個表格視圖單元格。然後,如果按下單元格,它將推動詳細視圖,並在文本字段中顯示單元格的title/text。在主視圖中,即時通訊使用NSFetchedResultsController。在第二個視圖中,即時嘗試加載數據,但出於某種原因,我沒有正確使用我自己的indexPath變量,因爲每個被按下的單元格都顯示詳細視圖上的第一個單元格text。代碼:與UITableView關聯的NSIndexPath

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = 
[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Set up the cell... 
[self configureCell:cell atIndexPath:indexPath]; 
self.path = indexPath; 
    //I have tried this as well. 
    //self.path = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; 
return cell; 
} 

詳情查看:

-(void)viewWillAppear:(BOOL)animated 
{ 
if (self.context == nil) 

{ 

    self.context = [(FBCDAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

} 

NSFetchRequest *request = [[NSFetchRequest alloc]init]; 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" inManagedObjectContext:self.context]; 

[request setEntity:entity]; 

NSError *error; 

NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy]; 

[self setTableArray:array]; 

FailedBankInfo *infoData = [self.tableArray objectAtIndex:self.root.path.row]; 

NSString *string = [NSString stringWithFormat:@"%@", infoData.name]; 

self.nameField.text = string; 

string = [NSString stringWithFormat:@"%@", infoData.city]; 

self.cityField.text = string; 

string = [NSString stringWithFormat:@"%@", infoData.state]; 

self.stateField.text = string; 

[super viewWillAppear:YES]; 

} 

回答

1

爲什麼不只是通過索引路徑進入詳細視圖控制器在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

方法,使用自定義初始化:

- (id)initDetailViewControllerWithIndexPath:(NSIndexPath*)indexPath 

我不熟悉UIViewController上的「root」屬性,那是什麼?它似乎是嘗試引用詳細視圖控制器中的第一個視圖控制器。你檢查過什麼self.root.path.row正在返回?

無論如何,您的第一個視圖控制器的路徑屬性與您選擇代碼的方式無關。它只是簡單地設置爲加載的最後一個單元格的indexPath。如果您確實想要如何從您的詳細信息視圖控制器訪問此屬性,則必須在didSelectRowAtIndexPath方法中設置它,而不是cellForRowAtIndexPath方法是準確的。


我認爲,如果你設置路徑didSelectRowAtIndexPath你的方式應該工作。但寫的自定義初始化,屬性添加到一個名爲路徑的詳細視圖控制器,然後添加自定義初始化到這樣的細節視圖控制器:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andIndexPath:(NSIndexPath*)indexPath 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.path = indexPath; 
    } 
    return self; 
} 

,並從第一個視圖的didSelectRowAtIndexPath方法方法調用此控制器使用

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle] andIndexPath:indexPath]; 
[self presentViewController:detailViewController animated:YES completion:nil]; 

(如果您沒有從xib初始化,請自定義您正在使用的初始化程序)。

或者,您可以傳遞包含您在詳細視圖控制器(標題和文本)中需要的數據的NSDictionary,而不是傳入索引路徑。

+0

謝謝你的回覆。通過將'indexPath'傳遞給詳細視圖,我該如何去做?我也嘗試過'didSelectRow ...'方法中的'self.path = [tableView indexPathForSelectedRow];''。是的,'root'是第一個視圖控制器。是的,我'NSLog'編輯了self.root.path.row並且無論按下什麼單元格都返回'0'。好吧,這很有道理。我將嘗試在'didSelectRowAtIndexPath'方法中設置'self.path'變量。再次感謝你! –

+0

我更新了答案。 – Darren

+0

+1,我希望我可以使用NSDictionary方法,但即時通訊使用核心數據和NSFetchedResultsController。但由於某種原因,這個該死的indexPath每次都堅持返回0。是否有另一種方法在視圖之間傳遞indexPath? (這會是問題嗎?)還是它被初始化的方式?謝謝! –