2013-11-20 103 views
0

我知道兩個UITableViews在一個UIViewController是一個常見的問題,但我還沒有找到解決方案,一個依賴於另一個的選擇。第二個UITableView填充在一個UIViewController上選擇另一個UITableView後

我有兩個UITableViews一個UIViewController。表1由具有不同名稱列表的CoreData實體填充。當我從表1中選擇一個名稱時,我希望表2中填寫與該人相關的所有記錄。

enter image description here

下面的代碼中,表1的工作原理是正確填充。然而,當我從表1中選擇一個名稱時,基於選擇(這是正確的)的數組進入表1而不是表2.

我也意識到,從表1中選擇的第二個名稱不會因爲它沒有區分選擇了哪個表格,所以工作很好。任何建議在這裏也歡迎。我已經讀過,標籤表是答案,但我沒有取得成功。

非常感謝提前。

誰能幫我

- (void)viewDidLoad 
{ 
tableLoad=1; 

[tableView setDelegate: self]; 
[tableView setDataSource: self]; 

.... code for populating teacherNames with names from Core data 

[self.tableView reloadData]; 
} 

-(void) loadSecondTable; 
{ 
[observationTableView setDelegate: self]; 
[observationTableView setDataSource: self]; 
tableLoad=2; 
[self.tableView reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if (tableLoad==1) 
{ 
return self.teacherNames.count; 
} 
else 
{ 
return self.observationNames.count; 
} 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (tableLoad == 1) 
{ 
static NSString *CellIdentifier = @"teacherCell"; 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
NSString *currentList = [[self.teacherNames objectAtIndex:indexPath.row] objectForKey:@"obsTeacherName"]; 
cell.textLabel.text = currentList; 
return cell; 
} 
else 
{ 
static NSString *CellIdentifier = @"observationCell"; 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
Observations *currentList = [self.observationNames objectAtIndex:indexPath.row]; 
cell.textLabel.text = [NSString stringWithFormat:@"Class %@ - %@ - %@", currentList.obsClassName, currentList.obsDate, currentList.obsDateTimeStart]; 
return cell; 
} 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(tableLoad==1) 
{ 
teacherChosenFromTable = [[self.teacherNames objectAtIndex:indexPath.row] objectForKey:@"obsTeacherName"]; 

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Observations"]; 
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"obsTeacherName" ascending:YES]]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"obsTeacherName == '%@'", teacherChosenFromTable]]; 
[fetchRequest setPredicate:predicate]; 
NSError *error = nil; 

self.observationNames = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
[self loadSecondTable]; 
} 

else 

{ 

.... load next view based on selection of Table 2 

} 
} 
+0

在loadSecondTable方法,你所謂的 「[self.tableView reloadData];」 將其更改爲「[observationTableView reloadData];」並再次嘗試。 – subchap

回答

1

,當你把它的滾動的IOS框架調用的數據源在需要的時候你不控制tableViews重繪。填充數據的算法必須考慮到這一點。您需要驗證哪個tableView調用委託。

試試這個:

- (void)viewDidLoad 
{ 
    [tableView setDelegate: self]; 
    [tableView setDataSource: self]; 

    [observationTableView setDelegate: self]; 
    [observationTableView setDataSource: self]; 


    .... code for populating teacherNames with names from Core data 

    [self.tableView reloadData]; 
} 

//-(void) loadSecondTable; 
//{ 
// [observationTableView setDelegate: self]; 
// [observationTableView setDataSource: self]; 
// [self.tableView reloadData]; 
//} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

//Caution you name your first tableView tableview and mask the parameter methode you need to rename it 
- (NSInteger)tableView:(UITableView *)p_TableView numberOfRowsInSection:(NSInteger)section 
{ 

    if (p_tableView == tableView) 
    { 
     return self.teacherNames.count; 
    } 
    else 
    { 
     return self.observationNames.count; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)p_TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (p_TableView == tableView) 
    { 
     static NSString *CellIdentifier = @"teacherCell"; 
     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     NSString *currentList = [[self.teacherNames objectAtIndex:indexPath.row] objectForKey:@"obsTeacherName"]; 
     cell.textLabel.text = currentList; 
     return cell; 
    } 
    else 
    { 
     static NSString *CellIdentifier = @"observationCell"; 
     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     Observations *currentList = [self.observationNames objectAtIndex:indexPath.row]; 
     cell.textLabel.text = [NSString stringWithFormat:@"Class %@ - %@ - %@", currentList.obsClassName, currentList.obsDate, currentList.obsDateTimeStart]; 
     return cell; 
    } 
} 

- (void)tableView:(UITableView *)p_TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (p_TableView == tableView) 
    { 
     teacherChosenFromTable = [[self.teacherNames objectAtIndex:indexPath.row] objectForKey:@"obsTeacherName"]; 

     NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Observations"]; 
     fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"obsTeacherName" ascending:YES]]; 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"obsTeacherName == '%@'", teacherChosenFromTable]]; 
     [fetchRequest setPredicate:predicate]; 
     NSError *error = nil; 

     self.observationNames = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
     [self.tableView reloadData]; 
    } 

    else 

    { 

     .... load next view based on selection of Table 2 

    } 
} 
+0

謝謝 - 我真的很感激你已經投入到這個時間。我會嘗試。 – RGriffiths

1

不要做標記。該方法被稱爲在同一個線程,但有一般沒有辦法正確地控制tableLoad狀態。所有數據源的方法有一個的tableView作爲參數,比較鏈接網點或變量,你應該引用保存到表視圖它們被初始化後的值的參數值。

區分在所述細胞被以相同的方式選擇的表圖。

+0

感謝您的回答 - 真正有用的 – RGriffiths

相關問題