Iphone 与UITableView关联的NSIndexPath
主视图上有一个表视图,详细视图上有一个文本字段。这个应用程序很简单。添加带有某些信息的表视图单元格。然后,如果按下单元格,它将推送详细视图,并在文本字段中显示单元格的Iphone 与UITableView关联的NSIndexPath,iphone,ios,core-data,nsfetchedresultscontroller,nsindexpath,Iphone,Ios,Core Data,Nsfetchedresultscontroller,Nsindexpath,主视图上有一个表视图,详细视图上有一个文本字段。这个应用程序很简单。添加带有某些信息的表视图单元格。然后,如果按下单元格,它将推送详细视图,并在文本字段中显示单元格的标题/文本。在主视图中,我使用NSFetchedResultsController。在第二个视图中,我试图加载数据,但由于某些原因,我没有正确使用自己的indexPath变量,因为每个被按下的单元格都会在细节视图上显示第一个单元格text。代码: - (UITableViewCell *)tableView:(UITableView
标题
/文本
。在主视图中,我使用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];
}
为什么不直接将索引路径传递到
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法,使用自定义初始值设定项:
- (id)initDetailViewControllerWithIndexPath:(NSIndexPath*)indexPath
?
我不熟悉UIViewController上的“root”属性,那是什么?这似乎是试图从局部视图控制器引用第一个视图控制器。是否检查了self.root.path.row返回的内容
无论如何,第一个视图控制器的路径属性独立于以代码方式选择的单元格。它只需设置为加载的最后一个单元格的indexPath。如果确实希望了解如何从详图视图控制器访问此属性,则必须在DidSelectRowatineXpath方法中设置该属性,而不是在CellForRowatineXpath方法中设置该属性才能准确
我认为,如果您在didSelectRowAtIndexPath中设置路径,您的方式应该会起作用。但要编写自定义初始值设定项,请向详图视图控制器添加名为path的属性,然后向详图视图控制器添加自定义初始值设定项,如下所示:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andIndexPath:(NSIndexPath*)indexPath
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.path = indexPath;
}
return self;
}
并使用从第一个视图控制器的didSelectRowAtIndexPath方法调用它
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle] andIndexPath:indexPath];
[self presentViewController:detailViewController animated:YES completion:nil];
(如果您不是从xib初始化,请自定义您正在使用的初始值设定项)
或者,您也可以不传入索引路径,而是传入一个NSDictionary,其中包含详细视图控制器中所需的数据(标题和文本)。谢谢您的回复。。通过
pass
将indepath
传递到详细视图中,我可以如何执行该操作?我还尝试了self.path=[tableView indexPathForSelectedRow]代码>在didSelectRow…
方法中。是,root
是第一个视图控制器。是的,我NSLog
-ed了self.root.path.row,无论按下哪个单元格,它都返回0
。好吧,这很有道理。我将尝试在didselectrowatinexpath
方法中设置self.path
变量。再次感谢你+我希望我可以使用NSDictionary方法,但是我使用的是核心数据和NSFetchedResultsController。但出于某种原因,这个该死的indexath坚持每次都返回0。是否有其他方法可以在视图之间传递indexPath?(这会是问题吗?)还是它的初始化方式?非常感谢。