简体   繁体   English

与UITableView关联的NSIndexPath

[英]NSIndexPath associated with UITableView

I have a table view on my master view, and a textField on my detail view. 我的主视图上有一个表格视图,详细视图上有一个textField。 The app is simple. 该应用程序很简单。 Add a table view cell with some info. 添加带有一些信息的表视图单元格。 Then if you press the cell, it pushes the detail view and shows the cell's title / text in the text field. 然后,如果您按该单元格,它将推入详细信息视图并在文本字段中显示该单元格的title / text In the master view, im using an NSFetchedResultsController. 在主视图中,即时消息使用NSFetchedResultsController。 And in the second view, im trying to load the data, but for some reason i am not using my own indexPath variable correctly, because every cell that gets pressed shows the first cells text on the detail view. 在第二个视图中,我试图加载数据,但是由于某种原因,我没有正确使用自己的indexPath变量,因为按下的每个单元格都在详细视图上显示了第一个单元格text Code: 码:

- (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;
}

Detail View: 详细视图:

-(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];

}

Why not just pass the index path into the detail view controller in the 为什么不直接将索引路径传递到的详细视图控制器中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

method, with a custom initializer: 方法,并带有自定义初始化程序:

- (id)initDetailViewControllerWithIndexPath:(NSIndexPath*)indexPath

?

I'm not familiar with a "root" property on a UIViewController, what is that? 我不熟悉UIViewController的“ root”属性,那是什么? It seems to be an attempt to reference the first view controller from the detail view controller. 似乎是尝试从详细视图控制器中引用第一个视图控制器。 Have you checked to see what self.root.path.row is returning? 您是否检查过返回的self.root.path.row?

Anyway the path property of your first view controller is independent of what ever cell is selected the way you have your code. 无论如何,第一个视图控制器的path属性与您使用代码的方式所选择的单元格无关。 It is simply set to the indexPath of the last cell that is loaded. 只需将其设置为最后加载的单元格的indexPath。 If you did want to some how access this property from your detail view controller, it would have to be set in the didSelectRowAtIndexPath method, not the cellForRowAtIndexPath method to be accurate. 如果您确实想通过详细视图控制器访问此属性,则必须在didSelectRowAtIndexPath方法中进行设置,而不是在cellForRowAtIndexPath方法中进行设置才能准确。


I think that if you set the path in didSelectRowAtIndexPath your way should work. 我认为,如果您在didSelectRowAtIndexPath中设置路径,则您的方法应该可行。 But to write the custom initializer, add a property to your detail view controller called path, and then add a custom initializer to the detail view controller like this: 但是要编写自定义初始化程序,请向详细信息视图控制器添加一个名为path的属性,然后将自定义初始化程序添加至详细信息视图控制器,如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andIndexPath:(NSIndexPath*)indexPath
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.path = indexPath;
    }
    return self;
}

and call this from the didSelectRowAtIndexPath method of the first view controller using 并使用以下方法从第一个视图控制器的didSelectRowAtIndexPath方法中调用它

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle] andIndexPath:indexPath];
[self presentViewController:detailViewController animated:YES completion:nil]; 

(if you're not initializing from a xib, customize which ever initializer that you're using). (如果您不是从xib初始化,请自定义所使用的初始化程序)。

Or, instead of passing in the index path, you could pass in an NSDictionary containing the data that you need in the detail view controller (the title and the text). 或者,您可以传入一个NSDictionary,而不是传递索引路径,该NSDictionary包含详细视图控制器中所需的数据(标题和文本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM