我知道兩個UITableViews
在一個UIViewController
是一個常見的問題,但我還沒有找到解決方案,一個依賴於另一個的選擇。第二個UITableView填充在一個UIViewController上選擇另一個UITableView後
我有兩個UITableViews
一個UIViewController
。表1由具有不同名稱列表的CoreData實體填充。當我從表1中選擇一個名稱時,我希望表2中填寫與該人相關的所有記錄。
下面的代碼中,表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
}
}
在loadSecondTable方法,你所謂的 「[self.tableView reloadData];」 將其更改爲「[observationTableView reloadData];」並再次嘗試。 – subchap