我知道一个 UIViewController
上有两个 UITableViews
的问题很常见,但我还没有找到一个解决方案依赖于另一个的选择。
我有一个带有两个 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
}
}
最佳答案
当您滚动 tableViews 时,您无法控制它的重绘,IOS 框架会在需要时调用 DataSource。填充数据的算法必须考虑到这一点。您需要验证哪个 tableView 调用委托(delegate)。
试试这个:
- (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
}
}
关于ios - 在一个 UIViewController 上选择另一个 UITableView 后填充第二个 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20105413/