我知道两个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