ios - indexPath 在 TableView 中没有值

标签 ios objective-c

我有一个显示联系人列表的 TableView ,当您单击一个单元格时,会弹出一个详细 View Controller ,其中包含显示联系人姓名、电话号码、传真和地址的标签。我的问题是,每次我单击一个单元格时,无论我单击哪个单元格,plist 中的第一个值都会弹出。当 NSLog(@"%@",indexPath); 每次都返回 null 时,我在 indexPath 中发现了错误。我认为问题出在这个方法中,但它在其他类中看起来是一样的并且工作正常。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showCountyInfo"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSLog(@"%@",indexPath);
        TSPBookCoverViewController *destViewController = segue.destinationViewController;
        destViewController.countyName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Name"];//error
        destViewController.phoneName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Phone"];
        destViewController.faxName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Fax"];
        destViewController.addressName = [[self.books objectAtIndex:indexPath.row] objectForKey:@"Address"];
    }
}

当我输入整数而不是 indexPath.row 时,它会正常工作,所以问题就在这里,我就是找不到它。很抱歉,如果很明显,我现在已经尝试寻找了一段时间!

编辑: 这是 didSelectRowAtIndexPath 方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Perform Segue
    [self performSegueWithIdentifier:@"showCountyInfo" sender:self];   
}

最佳答案

问题在于以下代码:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

您正在取消选择 didSelectRowAtIndexPath: 方法中的单元格。所以 indexPathForSelectedRow 将始终返回 nil

关于ios - indexPath 在 TableView 中没有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33023210/

相关文章:

ios - Phonegap 3.0 电源管理

android - 如何通过点击 Flutter 应用程序中的按钮使用默认应用程序打开 PPT、PDF、文档等?

ios - subview Controller 向下移动

ios - 通过 segue 传递子类的最佳方法是什么?

ios - 如何将透视变换应用于 UIView?

iOS 对时间 NSString 的 NSArray 进行排序

iOS - UINavigationController - NavigationBar 与 UIViewController 一起滑入

objective-c - 检测 UITableView 中的水平平移

iphone - 基本 Objective-C 变量声明

ios - 如何在不使用segue的情况下将数据从一个ViewController传递到MainViewController?