2012-02-20 71 views
0

我有一個UITableView顯示來自plist文件的數據。對於這個表格視圖,我簡單地從plist文件的內容中提取NSArray並將數據輸入到每個單元格中。但是當一個單元被點擊時,我需要下一個表視圖控制器來知道被點擊的單元格的索引路徑,以便這個視圖控制器知道數組中的哪個項目要顯示細節。我嘗試以下方法:在UITableViews之間傳遞NSIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    staffDetailView = [[StaffDetailViewController alloc] init]; 
    [staffDetailView setSelectedIndexPath:indexPath]; 

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

以上,我試圖抽頭單元的索引路徑傳遞給我在新的視圖控制器創建的NSIndexPath對象。

下表視圖控制器的頭文件:

@interface StaffDetailViewController : UITableViewController { 

    NSIndexPath *selectedIndexPath; 
    NSArray *staffArray; 

} 

@property (nonatomic, retain) NSIndexPath *selectedIndexPath; 
@property (nonatomic, strong) NSArray *staffArray; 

在這裏,我定義在前面視圖控制器設定的selectedIndexPath對象。

但是,當我嘗試訪問selectedIndexPath對象以顯示相關數據,對象似乎是nil

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    NSString *header; 
    if (section == 0) { 
     NSString *name = [[staffArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Staff Member"]; 
     NSArray *splitStaffArray = [name componentsSeparatedByString:@", "]; 
     NSString *lastName = [splitStaffArray objectAtIndex:0]; 
     NSString *firstName = [splitStaffArray objectAtIndex:1]; 
     NSString *consolidatedName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
     NSString *assignment = [[staffArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Assignment"]; 
     header = [NSString stringWithFormat:@"%@\n%@", consolidatedName, assignment]; 
     return header; 
    } 
    return nil; 
} 

上午我訪問selectedIndexPath對象不正確?有沒有一種優越的方法將正確的索引路徑傳遞給下一個視圖控制器?

+1

爲什麼不簡單地轉發對象實例而不是它的indexPath? – Till 2012-02-20 19:01:54

+0

我有多個實例要顯示的對象,我認爲出於效率考慮,最好只傳遞索引路徑並讓下一個視圖控制器負責檢索數據。不過,我會再試一次。 – 2012-02-20 19:04:02

+0

採取該方法返回null。 – 2012-02-20 19:07:44

回答

0

我修改了第一表視圖控制器的didSelectRowAtIndexPath方法:

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

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    [staffDetailView setSelectedIndexPath:indexPath]; 
    [staffDetailView setPassedName:[[staffArray objectAtIndex:indexPath.row] objectForKey:@"Staff Member"]]; 

} 

由於某種原因,我似乎需要在訪問視圖控制器的實例變量之前執行segue。奇怪的是,我不能只是視圖控制器它的自我alloc init。無論如何,索引路徑現在正在跨視圖控制器正確推送。

-3
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //I ll use NSUserDefaults to store tha indexpath. Indexpath.row is an int property, so convert to NSNumber 

    NSNumber *ip = [NSNumber numberWithInt:indexpath.row]; 
    NSUserDefaults *saveIP=[NSUserDefaults standartuserdefaults]; 
    [saveIP setObject: ip foKey:@"theIndexPath"]; 

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    [staffDetailView setSelectedIndexPath:indexPath]; 
    [staffDetailView setPassedName:[[staffArray objectAtIndex:indexPath.row] objectForKey:@"Staff Member"]]; 

} 

我其他視圖控制器檢索與代碼保存對象(數據)以下

NSNumber *theIndexpath= [[NSUserDefaults standartuserdefaults] objectForKey:@"theIndexPath"]; 

注:NSUserDefaults的可能不是一個好主意,但它是非常容易的,仍然可以工作,我的應用程序

+2

這是可怕的代碼,'NSUserDefaults'應該只用於設置,不是任意的數據,而且,由於你使用的鍵與不是唯一的整數相對應,所以你將自己侷限於整個應用程序的一個tableview。但更明顯的是,你的代碼中有很多拼寫錯誤會使得這個無法編譯。 – 2012-02-22 01:58:02