iphone - 与 UITableView 关联的 NSIndexPath

标签 iphone ios core-data nsfetchedresultscontroller nsindexpath

我的主视图上有一个 TableView ,而我的详细 View 上有一个文本字段。该应用程序很简单。添加包含一些信息的 TableView 单元格。然后,如果您按下该单元格,它会推送详细信息 View 并在文本字段中显示单元格的 title/text。在主视图中,我使用 NSFetchedResultsController。在第二个 View 中,我尝试加载数据,但由于某种原因,我没有正确使用自己的 indexPath 变量,因为按下的每个单元格都会在详细信息 View 上显示第一个单元格 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;
}

详细 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];

}

最佳答案

为什么不直接将索引路径传递到详细 View Controller 中

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

方法,使用自定义初始值设定项:

- (id)initDetailViewControllerWithIndexPath:(NSIndexPath*)indexPath

?

我不熟悉 UIViewController 上的“root”属性,那是什么?这似乎是尝试从详细 View Controller 引用第一个 View Controller 。您是否检查过 self.root.path.row 返回的内容?

无论如何,第一个 View Controller 的路径属性与您的代码选择的单元格无关。它只是设置为加载的最后一个单元格的索引路径。如果您确实想了解如何从详细信息 View Controller 访问此属性,则必须在 didSelectRowAtIndexPath 方法中设置它,而不是在 cellForRowAtIndexPath 方法中设置才准确。


我认为如果您在 didSelectRowAtIndexPath 中设置路径,您的方式应该可以工作。但是要编写自定义初始值设定项,请向详细 View Controller 添加一个名为 path 的属性,然后向详细 View Controller 添加自定义初始值设定项,如下所示:

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

并使用第一个 View Controller 的 didSelectRowAtIndexPath 方法调用此方法

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

(如果您不是从 xib 初始化,请自定义您正在使用的初始化程序)。

或者,您可以传入一个包含详细 View Controller 中所需数据(标题和文本)的 NSDictionary,而不是传入索引路径。

关于iphone - 与 UITableView 关联的 NSIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13790951/

相关文章:

iphone - 自定义UISlider(跟踪图片高度)

ios - 显示缩放 iOS 9 [调整大小]

ios - 测试 UITableViewCell 是否已被删除

iPhone - 当键盘出现时如何滚动 UITextField 并调整 UITextView 的大小

ios - 错误: Invalid resource directory name for splash

ios - 如何在 Swift 中为每个 UI 测试断言从空核心数据开始?

objective-c - 在预先存在的数据上设置关系是否可能?

Swift:对核心数据实体数组的核心数据提取请求

ios - 如何在 iOS 的共享首选项中保存 cookie?

iphone - UITextView 只是水平滚动帮助