我的主视图上有一个表格视图,我的详细视图上有一个文本字段。该应用程序很简单。添加包含一些信息的表格视图单元格。然后,如果您按下单元格,它会推动详细视图并在文本字段中显示单元格的title
/ 。text
在主视图中,我使用的是 NSFetchedResultsController。在第二个视图中,我试图加载数据,但由于某种原因,我没有正确使用我自己的 indexPath 变量,因为每个被按下的单元格都会显示text
详细视图上的第一个单元格。代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
self.path = indexPath;
//I have tried this as well.
//self.path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
return cell;
}
详细视图:
-(void)viewWillAppear:(BOOL)animated
{
if (self.context == nil)
{
self.context = [(FBCDAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" inManagedObjectContext:self.context];
[request setEntity:entity];
NSError *error;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
[self setTableArray:array];
FailedBankInfo *infoData = [self.tableArray objectAtIndex:self.root.path.row];
NSString *string = [NSString stringWithFormat:@"%@", infoData.name];
self.nameField.text = string;
string = [NSString stringWithFormat:@"%@", infoData.city];
self.cityField.text = string;
string = [NSString stringWithFormat:@"%@", infoData.state];
self.stateField.text = string;
[super viewWillAppear:YES];
}