2013-11-20 71 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

相关问题